在 Python 中使用非 ASCII 字符对邮件主题 (SMTP) 进行编码

发布于 2024-11-27 12:22:13 字数 1111 浏览 1 评论 0原文

我使用 Python 模块 MimeWriter 构建消息,并使用 smtplib 发送邮件 构建的消息是:

file msg.txt:
-----------------------
Content-Type: multipart/mixed;
from: me<[email protected]>
to: [email protected]
subject: 主題

Content-Type: text/plain;charset=utf-8

主題

我使用下面的代码发送邮件:

import smtplib
s=smtplib.SMTP('smtp.abc.com')
toList = ['[email protected]']
f=open('msg.txt') #above msg in msg.txt file
msg=f.read()
f.close()
s.sendmail('[email protected]',toList,msg)

我正确获取邮件正文,但主题不正确,

subject: some junk characters

主題           <- body is correct.

请提出建议?有没有办法指定用于主题的解码, 正如为身体指定的那样。如何正确解码主题?

I am using Python module MimeWriter to construct a message and smtplib to send a mail constructed message is:

file msg.txt:
-----------------------
Content-Type: multipart/mixed;
from: me<[email protected]>
to: [email protected]
subject: 主題

Content-Type: text/plain;charset=utf-8

主題

I use the code below to send a mail:

import smtplib
s=smtplib.SMTP('smtp.abc.com')
toList = ['[email protected]']
f=open('msg.txt') #above msg in msg.txt file
msg=f.read()
f.close()
s.sendmail('[email protected]',toList,msg)

I get mail body correctly but subject is not proper,

subject: some junk characters

主題           <- body is correct.

Please suggest? Is there any way to specify the decoding to be used for the subject also,
as being specified for the body. How can I get the subject decoded correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年慕年 2024-12-04 12:22:14

来自 http://docs.python.org/library/email.header.html

from email.message import Message
from email.header import Header
msg = Message()
msg['Subject'] = Header('主題', 'utf-8')
print msg.as_string()

主题:=?utf-8?b?5Li76aGM?=

更简单:

from email.header import Header
print Header('主題', 'utf-8').encode()

=?utf-8?b?5Li76aGM?=

作为补码解码可以使用:

from email.header import decode_header
a = decode_header("""=?utf-8?b?5Li76aGM?=""")[0]
print(a[0].decode(a[1]))

参考:
Python - 电子邮件标头解码 UTF-8

From http://docs.python.org/library/email.header.html

from email.message import Message
from email.header import Header
msg = Message()
msg['Subject'] = Header('主題', 'utf-8')
print msg.as_string()

Subject: =?utf-8?b?5Li76aGM?=

more simple:

from email.header import Header
print Header('主題', 'utf-8').encode()

=?utf-8?b?5Li76aGM?=

as complement decode may made with:

from email.header import decode_header
a = decode_header("""=?utf-8?b?5Li76aGM?=""")[0]
print(a[0].decode(a[1]))

Reference:
Python - email header decoding UTF-8

梨涡少年 2024-12-04 12:22:14

主题作为 SMTP 标头传输,并且它们必须仅是 ASCII。为了支持主题中的编码,您需要在主题前加上您想要使用的任何编码的前缀。对于您的情况,我建议在主题前加上“UTF-8”B?这意味着 UTF-8Base64 编码。

换句话说,我相信你的主题头应该或多或少看起来像这样:

Subject: =?UTF-8?B?JiMyMDAyNzsmIzM4OTg4Ow=?=

在 PHP 中,你可以这样处理:

// Convert subject to base64
$subject_base64 = base64_encode($subject);
fwrite($smtp, "Subject: =?UTF-8?B?{$subject_base64}?=\r\n");

在 Python 中:

import base64
subject_base64 = base64.encodestring(subject).strip()
subject_line = "Subject: =?UTF-8?B?%s?=" % subject_base64

The subject is transmitted as an SMTP header, and they are required to be ASCII-only. To support encodings in the subject you need to prefix the subject with whatever encoding you want to use. In your case, I would suggest prefix the subject with ?UTF-8?B? which means UTF-8, Base64 encoded.

In other words, I believe your subject header should more or less look like this:

Subject: =?UTF-8?B?JiMyMDAyNzsmIzM4OTg4Ow=?=

In PHP you could go about it like this:

// Convert subject to base64
$subject_base64 = base64_encode($subject);
fwrite($smtp, "Subject: =?UTF-8?B?{$subject_base64}?=\r\n");

In Python:

import base64
subject_base64 = base64.encodestring(subject).strip()
subject_line = "Subject: =?UTF-8?B?%s?=" % subject_base64
美胚控场 2024-12-04 12:22:14

简而言之,如果您使用 EmailMessage API,您应该像这样编写代码:

from email.message import EmailMessage
from email.header import Header
msg = EmailMessage()
msg['Subject'] = Header('主題', 'utf-8').encode()

@Sérgio 的回答不能在 EmailMessage API 中使用,因为仅字符串对象< /strong> 可以分配给 EmailMessage()["Subject"],但不能分配给 email.header.Header 对象。

In short, if you use the EmailMessage API, you should code like this:

from email.message import EmailMessage
from email.header import Header
msg = EmailMessage()
msg['Subject'] = Header('主題', 'utf-8').encode()

Answer from @Sérgio cannot be used in the EmailMessage API, cause only string object can be assigned to EmailMessage()["Subject"], but not an email.header.Header object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文