检测电子邮件是否为“传送状态通知”并提取信息 - Python [英] Detecting if an email is a "Delivery Status Notification" and extract information - Python

查看:423
本文介绍了检测电子邮件是否为“传送状态通知”并提取信息 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the Python email module to parse emails.

我需要能够告诉如果电子邮件是传送状态通知,请查看状态是什么,并提取失败的电子邮件中的信息,例如。主题。

I need to be able to tell if an email is a "Delivery Status Notification", find out what the status is, and extract information on the email that failed, eg. the Subject.

我使用.parsestr(email)解析后得到的对象是这样的:

The object I get after parsing with .parsestr(email) is like this:

{'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'}

首先,我如何知道这是一个DSN,而不使用这个主题的正则表达式?

Firstly, how do I tell that this is a DSN without using a regexp on the subject?

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

Secondly, how do I access the body of the email, and information such as the error that was returned by the mail server?

编辑: 确定了我需要使用 .get_payload()来获取消息的内容。

edit: worked out I need to use .get_payload() to get the contents of the message.

电子邮件文档说:


Parser类在其公共接口上没有差别。
有一些额外的智能
识别消息/传递状态类型
消息
,它表示为
包含单独$ b的消息实例$ b发送状态通知中每个头块
的消息子部分






更新:



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


Update:

Basically, I need to be able to reliable detect that an email is a DSN, and then also to extract the original message so I can parse that with email.Parser() and get information about it.

推荐答案

您引用的文档说,如果是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



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



Format of a Delivery Status Notification (from 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.

这篇关于检测电子邮件是否为“传送状态通知”并提取信息 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆