我正在编写一个 Python 脚本来处理从Procmail http://www.procmail.org。正如本文中所建议的question https://stackoverflow.com/questions/1225586/checking-email-with-python,我使用以下 Procmail 配置:
:0:
|$HOME/process_mail.py
我的 process_mail.py 脚本通过 stdin 接收电子邮件,如下所示:
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] /cdn-cgi/l/email-protection>; 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] /cdn-cgi/l/email-protection>
Subject: TEST 12
From: Full Name <[email protected] /cdn-cgi/l/email-protection>
To: [email protected] /cdn-cgi/l/email-protection
Content-Type: text/plain; charset=ISO-8859-1
ONE
TWO
THREE
我试图以这种方式解析该消息:
>>> import email
>>> msg = email.message_from_string(full_message)
我想要获取“发件人”、“收件人”和“主题”等消息字段。但是,消息对象不包含任何这些字段。
我究竟做错了什么?
您必须确保这些行不会意外中断(如上面所示,尽管很难说这是否是复制粘贴问题)——并带有完整的消息,例如:
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] /cdn-cgi/l/email-protection>; 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] /cdn-cgi/l/email-protection>
Subject: TEST 12
From: Full Name <[email protected] /cdn-cgi/l/email-protection>
To: [email protected] /cdn-cgi/l/email-protection
Content-Type: text/plain; charset=ISO-8859-1
ONE
TWO
THREE
then
msg = email.message_from_string(msgtxt)
print msg['Subject']
prints TEST 12
如预期的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)