如何使用quoted-printable transfer-encoding和utf-8内容编码发送电子邮件?

Jam*_*979 2 python email smtp

python email.mime倾向于使用编码base647bitus-ascii.我想用quoted-printableutf-8.

目前,我的电子邮件看起来像

--===============6135350048414329636==
Content-Type: text/plain
MIME-Version: 1.0
Content-Transfer-Encoding: base64

IyEvYmluL2Jhc2gKCmZvciBpIGluIHs4Mjg4Li44N
Run Code Online (Sandbox Code Playgroud)

或者,在某些情况下,

--===============0756888342500148236==
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

foobar
Run Code Online (Sandbox Code Playgroud)

我希望原始电子邮件更易于人类阅读.我也更喜欢utf-8编码.

Jam*_*979 7

简短的回答

设置内容传输编码

创建MIMEText将附加到MIMEMultipart对象的对象时,首先设置content-transfer-encoding为值quoted-printable,然后执行set_payload.

# first create MIMEText, then set content-transfer-encoding, then set payload
mt = MIMEText(None, _subtype='plain')
mt.replace_header('content-transfer-encoding', 'quoted-printable')
mt.set_payload(u'happy face ?', 'utf-8')

# create the parent email object and the MIMEMultipart extension to it
email = MIMEMultipart('mixed')
inline = MIMEMultipart('alternative')

# assemble the objects
inline.attach(mt)
email.attach(inline)
Run Code Online (Sandbox Code Playgroud)

设置电子邮件字符集和电子邮件编码

cs = charset.Charset('utf-8')
cs.header_encoding = charset.QP
cs.body_encoding = charset.QP
email.set_charset(cs)
Run Code Online (Sandbox Code Playgroud)

长脚本示例

以下是一个较长的脚本,为先前的代码片段提供更多上下文.

此脚本将发送text/plain以UTF-8编码的部分.为了好玩,它还会附加一个文件.这产生的原始电子邮件将是人类可读的(文件附件除外).

from __future__ import print_function

from email.mime.multipart import MIMEMultipart
from email import charset

# create the parent email object
email = MIMEMultipart('mixed')
# set email charset and email encodings
cs_ = charset.Charset('utf-8')
cs_.header_encoding = charset.QP
cs_.body_encoding = charset.QP
email.set_charset(cs_)

# create the 'text/plain' MIMEText
from email.mime.text import MIMEText
# first create MIMEText, then set content-transfer-encoding, then set payload
mt = MIMEText(None, _subtype='plain')
mt.replace_header('content-transfer-encoding', 'quoted-printable')
mt.set_payload(u'happy face ?', 'utf-8')

# assemble the parts
inline = MIMEMultipart('alternative')
inline.attach(mt)
email.attach(inline)

# for fun, attach a file to the email
import mimetypes
from email.mime.base import MIMEBase
from email.encoders import encode_base64
my_file = '/tmp/test.sh'
mimetype, encoding = mimetypes.guess_type(my_file)
mimetype = mimetype or 'application/octet-stream'
mimetype = mimetype.split('/', 1)
attachment = MIMEBase(mimetype[0], mimetype[1])
attachment.set_payload(open(my_file, 'rb').read())
encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(my_file))
email.attach(attachment)
Run Code Online (Sandbox Code Playgroud)

这将创建一个人类可读的原始电子邮件(base64编码的文件附件除外)

--===============5610730199728027971==
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

happy face ?

--===============5610730199728027971==--

--===============0985725891393820576==
Content-Type: text/x-sh
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.sh"

Zm9vYmFyc2RmYXNkZmtqaGFzZGZrbGhhc2ZrbGpoYXNma2xqaGFzZmtsaGZkYXNmCg==

--===============0985725891393820576==--
Run Code Online (Sandbox Code Playgroud)

(奖金)发送电子邮件

使用smtplib,可以通过电子邮件发送电子邮件.

# set email address headers
email['From'] = 'me@email.com'
email['To'] = 'you@email.com'
email['Subject'] = 'hello'

# send the email
import smtplib
smtp_srv = smtplib.SMTP('localhost')
smtp_srv.set_debuglevel(True)
print(mesg_html, end='\n\n')
print(email.as_string(), end='\n\n')
smtp_srv.sendmail('me@email.com', 'you@email.com', email.as_string())
smtp_srv.quit()
Run Code Online (Sandbox Code Playgroud)

  • 该示例似乎根本没有引用可打印编码。正确的 QP 看起来像 `happy face =E2=98=BA` —— 或者,在 Python 的默认 QP 实现中对空格进行令人讨厌的过度编码,`happy=20face=20=E2=98=BA`。 (2认同)