Google app engine 如何在AppEngine管理控制台中记录电子邮件详细信息?
我从我的google帐户向我的GAE电子邮件发送消息:Google app engine 如何在AppEngine管理控制台中记录电子邮件详细信息?,google-app-engine,email,encoding,python-2.5,Google App Engine,Email,Encoding,Python 2.5,我从我的google帐户向我的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.subje
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: ������, ��� �������� ���������
如果我这样做:
body = pl.payload.decode(pl.encoding).decode('koi8-r')
然后正确显示消息正文。但是我应该如何提取编码呢?因此,我使用了以下解决方案:
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下)中正确显示俄文文本:
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')
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)