首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python使用"quoted-printable“传输编码和"utf-8”内容编码发送电子邮件

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

Stack Overflow用户
提问于 2015-07-30 10:24:44
回答 2查看 9.1K关注 0票数 5

python的email.mime倾向于使用编码base647bitus-ascii。我想使用quoted-printableutf-8,因为这对人类更容易阅读和调试。

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

代码语言:javascript
复制
--===============6135350048414329636==
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: base64

IyEvYmluL2Jhc2gKCmZvciBpIGluIHs4Mjg4Li44N

代码语言:javascript
复制
--===============0756888342500148236==
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

happy face =E2=98=BA

我希望原始的电子邮件是在引用可打印的unicode,以便它更容易为人们阅读。

代码语言:javascript
复制
--===============5610730199728027971==
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

happy face ☺
EN

回答 2

Stack Overflow用户

发布于 2015-07-30 10:24:44

简短的回答

设置content-transfer-encoding

在创建将附加到MIMEMultipart对象的MIMEText对象时,首先将content-transfer-encoding设置为值quoted-printable,然后执行set_payload。操作的顺序很重要。

代码语言:javascript
复制
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和各种编码

代码语言:javascript
复制
cs = charset.Charset('utf-8')
cs.header_encoding = charset.QP
cs.body_encoding = charset.QP
email.set_charset(cs)

结果

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

代码语言:javascript
复制
>>> 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节。为了好玩,它还会附加一个文件。这样产生的原始电子邮件将是人类可读的(文件附件除外)。

代码语言:javascript
复制
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)

结果

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

代码语言:javascript
复制
>>> 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,可以通过电子邮件发送电子邮件。

代码语言:javascript
复制
import smtplib

# set email address headers
email['From'] = 'me@email.com'
email['To'] = 'you@email.com'
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('me@email.com', 'you@email.com', email.as_string())
smtp_srv.quit()
票数 10
EN

Stack Overflow用户

发布于 2020-04-15 03:27:44

在尝试更改现有消息(email.Message对象)的正文并将其编码设置为quoted-printable时,我发现这个问题花费的精力远远超出了我的预期。

代码语言:javascript
复制
import email
#... 'part' is the Message object
content = part.get_payload(decode=True)
#... Modify content
part['Content-Transfer-Encoding'] = '8bit'
part.set_payload(content, 'UTF-8')
del part['Content-Transfer-Encoding']
email.encoders.encode_quopri(part)

现在,为什么我要设置然后删除Content-Transfer-Encoding头呢?set_payload调用将设置Content-Transfer-Encoding标头,如果不存在标头,则将数据编码为Base64。否则,set_payload调用将假定调用者已经对数据进行了编码,并且不会对其进行更改(通过编码)。因此,实际上我将Content-Transfer-Encoding头设置为什么值并不重要,只是我没有将其留空。

但是为什么我需要删除头部呢?email.encoders.encode_quopri调用将只添加一个标头,因此消息将具有多个Content-Transfer-Encoding标头。

因此,对没有Content-Transfer-Encoding头的消息使用set_payload然后使用encode_quopri将导致Base64字符串的带引号的可打印表示形式,而对于具有现有Content-Transfer-Encoding头的消息将导致具有重复头的消息。使用encode_quopri然后使用set_payload可能会导致重复的报头,但不会对消息进行编码。因此,为了避免陷入the quopri module,需要添加/删除繁琐的语句。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31714221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档