我正在使用Pythonemail
解析电子邮件的模块。
我需要能够判断电子邮件是否是“传递状态通知”,找出状态是什么,并提取有关失败电子邮件的信息,例如。主题。
用 .parsestr(email) 解析后得到的对象是这样的:
{'Content-Transfer-Encoding': 'quoted-printable',
'Content-Type': 'text/plain; charset=ISO-8859-1',
'Date': 'Mon, 14 Mar 2011 11:26:24 +0000',
'Delivered-To': '[email protected] /cdn-cgi/l/email-protection',
'From': 'Mail Delivery Subsystem <[email protected] /cdn-cgi/l/email-protection>',
'MIME-Version': '1.0',
'Message-ID': '<[email protected] /cdn-cgi/l/email-protection>',
'Received': 'by 10.142.13.8 with SMTP id 8cs63078wfm;\r\n Mon, 14 Mar 2011 04:26:24 -0700 (PDT)',
'Return-Path': '<>',
'Subject': 'Delivery Status Notification (Failure)',
'To': 'se[email protected] /cdn-cgi/l/email-protection',
'X-Failed-Recipients': '[email protected] /cdn-cgi/l/email-protection'}
Firstly,如何在不使用正则表达式的情况下判断这是一个 DSN?
Secondly、如何访问电子邮件正文以及邮件服务器返回的错误等信息?
edit:解决了我需要使用.get_payload()
获取消息的内容。
The 电子邮件文档 http://docs.python.org/library/email#differences-from-mimelib say:
Parser 类的公共接口没有区别。确实如此
有一些额外的智慧
识别消息/传递状态类型
消息,它表示为
包含单独的消息实例
每个标头块的消息子部分
在交货状态通知中
Update:
基本上,我需要能够可靠地检测电子邮件是否为 DSN,然后提取原始消息,以便我可以使用 email.Parser() 对其进行解析并获取有关它的信息。