GAE Python - 如何在邮件正文中编码特殊字符?

时间:2013-03-26 10:39:12

标签: python google-app-engine email mime

我想在谷歌应用引擎上发送带有python的订单通知。 问题是邮件正文可能包含像'öäü'这样的特殊字符,但我没有机会从内容类型修改字符集。

是否有可能将字符集从charset =“us-ascii”更改为“utf-8”并仍使用google app engine mail api或解决方法?喜欢添加参数Content-transfer-encoding:quoted-printable?

这是我发送通知的方法:

from google.appengine.ext import db
from google.appengine.api import mail

from email.header import Header

def encode_mail_header(line):
    return Header(line, 'utf-8').encode()

msg = u"Message with some chars like öäüßéèô..."
subject = encode_mail_header(u"Hans Müller your Ticket")
sender = "My Service <notification+@localhost.de>"
to = encode_mail_header(u"Hans Müller")
to += " <hans.mueller@localhost>"

message = mail.EmailMessage(sender=sender,
      to=to,
      subject=subject,
      body=msg)
message.send()

从我的开发服务器收到电子邮件代码:

Received: from spooler by localhost (Mercury/32 v4.62); 26 Mar 2013 10:50:35 +0100
X-Envelope-To: <hans.mueller@localhost>
Return-path: <notification+@localhost.de>
Received: from [192.168.56.1] (127.0.0.1) by localhost (Mercury/32 v4.62) with ESMTP ID MG000011;
   26 Mar 2013 10:50:24 +0100
Content-Type: multipart/mixed; boundary="===============1598388400=="
MIME-Version: 1.0
To: =?utf-8?q?Hans_M=C3=BCller?= <hans.mueller@localhost>
From: My Service <notification+@localhost.de>
Reply-To:
Subject: =?utf-8?q?Hans_M=C3=BCller_your_Ticket?=
X-UC-Weight: [#   ] 51
X-CC-Diagnostic: Not Header "Date" Exists (51)

--===============1598388400==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

Message with some chars like öäüßéèô...
--===============1598388400==--

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我们使用以下代码:

message = mail.EmailMessage()

message.subject =“=?utf-8?B?%s?=”%base64.b64encode(u“üäö”.encode(“UTF-8”))

message.html = u“üäö”.encode('ascii','xmlcharrefreplace')