简短回答
设置content-transfer-encoding
创建将附加到MIMEMultipart
对象的MIMEText
对象时,首先将content-transfer-encoding
设置为值quoted-printable
,然后执行set_payload
。操作顺序很重要。
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mt = MIMEText(None, _subtype='plain')
mt.replace_header('content-transfer-encoding', 'quoted-printable')
mt.set_payload(u'happy face ☺', 'utf-8')
email = MIMEMultipart('mixed')
inline = MIMEMultipart('alternative')
inline.attach(mt)
email.attach(inline)
设置电子邮件的字符集和各种编码方式
cs = charset.Charset('utf-8')
cs.header_encoding = charset.QP
cs.body_encoding = charset.QP
email.set_charset(cs)
结果
这将创建一封原始邮件,其内容对人类可读(除了 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==--
长篇回答
以下是更长的脚本,以提供有关先前代码片段的更多背景。
此脚本将发送以UTF-8编码的text/plain
部分。 为了好玩,它还将附加一个文件。
这将产生可读的原始电子邮件(除了文件附件)。
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
email = MIMEMultipart('mixed')
cs_ = charset.Charset('utf-8')
cs_.header_encoding = charset.QP
cs_.body_encoding = charset.QP
email.set_charset(cs_)
mt = MIMEText(None, _subtype='plain')
mt.replace_header('content-transfer-encoding', 'quoted-printable')
mt.set_payload(u'happy face ☺', 'utf-8')
inline = MIMEMultipart('alternative')
inline.attach(mt)
email.attach(inline)
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)
结果
这将创建一封原始的电子邮件,可以被人类阅读(除了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==--
(额外奖励)发送电子邮件
使用smtplib
,可以将电子邮件发送。
import smtplib
email['From'] = 'me@email.com'
email['To'] = 'you@email.com'
email['Subject'] = 'hello'
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()