Python 使用“quoted-printable”传输编码和“utf-8”内容编码发送电子邮件

2023-11-23

蟒蛇的email.mime倾向于使用编码base64 or 7bit and us-ascii。我想用quoted-printable and utf-8因为这对于人类来说更容易阅读和调试。

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

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

IyEvYmluL2Jhc2gKCmZvciBpIGluIHs4Mjg4Li44N

or

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

happy face =E2=98=BA

我希望原始电子邮件采用可引用打印的 unicode 格式,以便人们更容易阅读。

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

happy face ☺

简短的回答

set content-transfer-encoding

创建时MIMEText对象,它将附加到MIMEMultipart对象,设置content-transfer-encoding重视quoted-printable首先,然后做set_payload。操作顺序很重要。

from email.mime.multipart import MIMEMultipart
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')

# 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)

设置电子邮件charset以及各种编码

cs = charset.Charset('utf-8')
cs.header_encoding = charset.QP
cs.body_encoding = charset.QP
email.set_charset(cs)

Result

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

>>> print(email)
--===============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==--

长答案

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

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

from __future__ import print_function

from email import charset    
from email.encoders import encode_base64
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes

# 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
# 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
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)

Result

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

>>> print(email)
--===============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==--

(奖励)发送电子邮件

Using smtplib,可以通过电子邮件发送电子邮件。

import smtplib

# set email address headers
email['From'] = '[email protected]'
email['To'] = '[email protected]'
email['Subject'] = 'hello'

# send the email
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('[email protected]', '[email protected]', email.as_string())
smtp_srv.quit()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 使用“quoted-printable”传输编码和“utf-8”内容编码发送电子邮件 的相关文章

随机推荐