【问题标题】:How to log e-mail details in AppEngine Admin console?如何在 App Engine 管理控制台中记录电子邮件详细信息?
【发布时间】:2013-04-15 17:27:02
【问题描述】:

我从我的谷歌帐户向我的 GAE 电子邮件发送消息:

Content-Type: text/plain; charset=KOI8-R
Content-Transfer-Encoding: base64

然后使用以下代码:

logging.debug('Received personal inbound mail from: %s, to: %s, subject: %s' % (mail_message.sender, mail_message.to, mail_message.subject))
body = ''
for b in mail_message.bodies('text/plain'):
    body_type, pl = b # content encoding, plain text
    if pl.encoding:
        body = pl.payload.decode(pl.encoding)
    else:
        body = pl.payload
if body:
    logging.debug('Inbound personal mail body: %s' % (body))

结果我在控制台中看到以下内容:

Received personal inbound mail from: Test <test@example.com>, to: test@myappid.appspotmail.com, subject: Readable russian text
Inbound personal mail body: ������, ��� �������� ���������

即主题可读,但正文不可读。

当我检查收到的内容时,我看到以下内容:

text/plain, From nobody Mon Apr 15 17:15:34 2013
content-transfer-encoding: base64
MIME-Version: 1.0
Content-Type: text/plain; charset="koi8-r"

8NLJ18XULCDc1M8g1MXT1M/Xz8Ug08/Pwt3

如果我这样做:

body = pl.payload.decode(pl.encoding).decode('koi8-r')

然后消息正文显示正确。但是我应该如何提取编码呢?

【问题讨论】:

    标签: google-app-engine email encoding python-2.5


    【解决方案1】:

    结果,我使用了以下解决方案:

    if msg.is_multipart():
        for part in msg.walk():
            if part.get_content_type() and part.get_content_type()=='text/plain': # ignore text/html
                charset = part.get_content_charset()
                body = part.get_payload(decode=True).decode(charset)
    else:
        body = msg.get_payload(decode=True)
        body = body.decode('utf-8')
    

    为了在开发者的管理控制台中正确显示俄语文本(在 Windows 7 下):

    def logging_debug(what):
        ''' Function to support cp866 encoding in developers admin console
        '''
        if os.environ.get('SERVER_SOFTWARE','').startswith('Devel'):
            logging.debug(what.encode('cp866'))
        else:
            logging.debug(what)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 2010-10-23
      • 2020-09-14
      • 2022-10-01
      • 2021-04-25
      • 1970-01-01
      相关资源
      最近更新 更多