检测电子邮件是否为"传递状态通知"并提取信息 - Python

Aco*_*orn 11 python email

我正在使用Python email模块来解析电子邮件.

我需要能够判断电子邮件是否为"传递状态通知",找出状态是什么,并提取有关失败的电子邮件的信息,例如.主题.

使用.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': 'sender@gmail.com',
 'From': 'Mail Delivery Subsystem <mailer-daemon@googlemail.com>',
 'MIME-Version': '1.0',
 'Message-ID': '<000e08jf90sd9f00e6f943f@google.com>',
 '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': 'sender@gmail.com',
 'X-Failed-Recipients': 'recipient@gmail.com'}
Run Code Online (Sandbox Code Playgroud)

首先,如何在不使用正则表达式的情况下判断这是DSN?

其次,如何访问电子邮件正文以及邮件服务器返回的错误等信息?

编辑:我需要.get_payload()用来获取消息的内容.

电子邮件文档说:

Parser类的公共接口没有差异.它确实有一些额外的智能来识别消息/传递状态类型消息,它表示为一个Message实例,它包含传递状态通知中每个头块的单独Message子部分


更新:

基本上,我需要能够可靠地检测到电子邮件是DSN,然后还要提取原始邮件,以便我可以使用email.Parser()解析它并获取有关它的信息.

jfs*_*jfs 17

您引用的文档说如果它是DSN,则该消息是多部分的:

import email

msg = email.message_from_string(emailstr)

if (msg.is_multipart() and len(msg.get_payload()) > 1 and 
    msg.get_payload(1).get_content_type() == 'message/delivery-status'):
    # email is DSN
    print(msg.get_payload(0).get_payload()) # human-readable section

    for dsn in msg.get_payload(1).get_payload():
        print('action: %s' % dsn['action']) # e.g., "failed", "delivered"

    if len(msg.get_payload()) > 2:
        print(msg.get_payload(2)) # original message
Run Code Online (Sandbox Code Playgroud)

交货状态通知的格式(来自rfc 3464):

A DSN is a MIME message with a top-level content-type of
multipart/report (defined in [REPORT]).  When a multipart/report
content is used to transmit a DSN:

(a) The report-type parameter of the multipart/report content is
    "delivery-status".

(b) The first component of the multipart/report contains a human-
    readable explanation of the DSN, as described in [REPORT].

(c) The second component of the multipart/report is of content-type
    message/delivery-status, described in section 2.1 of this
    document.

(d) If the original message or a portion of the message is to be
    returned to the sender, it appears as the third component of the
    multipart/report.
Run Code Online (Sandbox Code Playgroud)

  • @ jf-sebastian标准很漂亮,但在现实世界中我们想要检测DSN,无论它是否符合RFC.正如我测试过的那样,没有多少DSN符合这个标准(我见过的是我的服务器,但就是这个). (6认同)
  • 问题是您不能依赖这些测试,因为DSN通常不会是多部分,并且不会有"内容类型"消息/传递状态',例如Gmail DSN. (4认同)