1

我喜欢在谷歌应用引擎上使用 python 发送订单通知。问题是邮件正文可能包含像“öäü”这样的特殊字符,但我找不到从内容类型修改字符集的机会。

是否有可能将字符集从 charset="us-ascii" 更改为 example 为 "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==--

感谢帮助

4

1 回答 1

1

我们使用以下代码:

消息 = 邮件.EmailMessage()

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

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

于 2013-03-26T15:18:44.153 回答