python - 使用 App Engine Python 接收非 ascii 电子邮件会导致奇怪的字符编码

标签 python google-app-engine email encoding ascii

我正在使用 Google App Engine Python 接收电子邮件并将其存储在数据存储区中。

class LogSenderHandler(InboundMailHandler):
    def receive(self, mail_message):
        logging.info("Received a message from: " + mail_message.sender)

当我收到来自 ASCII 地址的电子邮件时,一切正常:

Me [me@gmail.com] 存储为 Me [me@gmail.com]

但是,如果发件人值包含非 ascii 字符,则存储的数据值如下所示:

Mr Kröber [mr.kroeber@gmail.com] 存储为 =?ISO-8859-1?Q?Mr_Kr=F6ber?= [mr.kroeber@gmail.com ]

最佳答案

主题根据RFC 2047编码.查看Internationalized headersemail模块。

这是一个将主题解码为 Unicode 的示例:

>>> s='=?ISO-8859-1?Q?Mr_Kr=F6ber?= [mr.kroeber@gmail.com]'
>>> from email.header import decode_header
>>> decode_header(s)
[('Mr Kr\xf6ber', 'iso-8859-1'), ('[mr.kroeber@gmail.com]', None)]
>>> u = u' '.join(w.decode(e or 'ascii') for w,e in decode_header(s))
>>> u
u'Mr Kr\xf6ber [mr.kroeber@gmail.com]'
>>> print u
Mr Kröber [mr.kroeber@gmail.com]

关于python - 使用 App Engine Python 接收非 ascii 电子邮件会导致奇怪的字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994347/

相关文章:

android - 从 Android Studio 部署 App Engine

android - java.lang.NoClassDefFoundError : com. google.firebase.FirebaseOptions

php - 使用代码通过 IMAP 将标签附加到 Gmail 中的邮件

java邮件轮询从邮件中读取内联或嵌入图像(笑脸)

python - 如何阻止 QThread 关闭?

python - 如何从字符串中删除\n 和\r

java - 如何在 App Engine v1.9.18 中将 app.yaml 与 Java 运行时一起使用?

javascript - 使用 mailto URL 将附件添加到新的 Outlook 电子邮件

python - 属性错误 : module 'fractions' has no attribute 'Fraction'

Python - 如何将变量分配给显示在单独行上的表格