【问题标题】:GAE Python - Howto encode special character in a mail body?GAE Python - 如何在邮件正文中编码特殊字符?
【发布时间】:2013-03-16 02:58:14
【问题描述】:

我喜欢在谷歌应用引擎上使用 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==--

感谢您的帮助

【问题讨论】:

    标签: python google-app-engine email mime


    【解决方案1】:

    我们使用以下代码:

    message = mail.EmailMessage()

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

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

    【讨论】:

    • 我想避免使用 html。好吧,当没有机会时……谢谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 2016-10-19
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2020-03-10
    • 2012-03-18
    相关资源
    最近更新 更多