我的设置使用 fetchmail 从 Gmail 中提取电子邮件,这些电子邮件由 procmail 处理并将其传递给 python 脚本。
当我使用email.message_from_string()
时,生成的对象不会被解析为电子邮件对象。 get_payload()
将电子邮件的标题/正文/有效负载文本作为单个文本块返回。
这是它返回的文本:
From example@gmail.com Sat Aug 17 19:20:44 2013
>From example Sat Aug 17 19:20:44 2013
MIME-Version: 1.0
Received: from ie-in-f109.1e100.net [74.125.142.109]
by VirtualBox with IMAP (fetchmail-6.3.21)
for <example@localhost> (single-drop); Sat, 17 Aug 2013 19:20:44 -0700 (PDT)
Received: by 10.70.131.110 with HTTP; Sat, 17 Aug 2013 19:20:42 -0700 (PDT)
Date: Sat, 17 Aug 2013 19:20:42 -0700
Delivered-To: example@gmail.com
Message-ID: <CAAsp4m0GBeVg80-ryFgNvNNAj_QPguzbX3DqvMSx-xSGZM18Pw@mail.gmail.com>
Subject: test 19:20
From: example <example@gmail.com>
To: example <example@gmail.com>
Content-Type: multipart/alternative; boundary=001a1133435474449004e42f7861
--001a1133435474449004e42f7861
Content-Type: text/plain; charset=ISO-8859-1
19:20
--001a1133435474449004e42f7861
Content-Type: text/html; charset=ISO-8859-1
<div dir="ltr">19:20</div>
--001a1133435474449004e42f7861--
我的代码:
full_msg = sys.stdin.read()
msg = email.message_from_string(full_msg)
msg['to'] # returns None
msg.get_payload() # returns the text above
为了让 Python 正确解释电子邮件,我缺少什么?