我正在编写一个Python脚本来处理从Procmail返回的电子邮件。在本question建议,我用下面的Procmail配置:用Python解析电子邮件
:0:
|$HOME/process_mail.py
我process_mail.py脚本通过标准输入接收电子邮件这样的:
From hostname Tue Jun 15 21:43:30 2010
Received: (qmail 8580 invoked from network); 15 Jun 2010 21:43:22 -0400
Received: from mail-fx0-f44.google.com (209.85.161.44)
by ip-73-187-35-131.ip.secureserver.net with SMTP; 15 Jun 2010 21:43:22 -0400
Received: by fxm19 with SMTP id 19so170709fxm.3
for <[email protected]>; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.103.84.1 with SMTP id m1mr2774225mul.26.1276652853684; Tue, 15
Jun 2010 18:47:33 -0700 (PDT)
Received: by 10.123.143.4 with HTTP; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)
Date: Tue, 15 Jun 2010 20:47:33 -0500
Message-ID: <[email protected]>
Subject: TEST 12
From: Full Name <[email protected]>
To: [email protected]
Content-Type: text/plain; charset=ISO-8859-1
ONE
TWO
THREE
我试图解析消息以这种方式:
>>> import email
>>> msg = email.message_from_string(full_message)
我想获得'From','To'和'Subject'等消息字段。但是,消息对象不包含任何这些字段。
我在做什么错?
如何获取电子邮件的正文? – Anuj 2014-02-10 13:09:43
如果你真的想要整个RFC2822电子邮件正文与原始MIME结构和所有,在Python解析消息基本上是多余的;身体是第一条空行之后的一切。通常,对于现代消息,您想要解析MIME结构并提取一个或多个身体部位。 – tripleee 2016-06-22 09:35:14