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

2024-06-22

我正在使用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() 对其进行解析并获取有关它的信息。


您引用的文档说 http://docs.python.org/library/email#differences-from-mimelib如果消息是多部分的DSN https://www.rfc-editor.org/rfc/rfc1894.html:

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 https://www.rfc-editor.org/rfc/rfc3464#page-7):

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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检测电子邮件是否为“递送状态通知”并提取信息 - Python 的相关文章

  • X 轴刻度标签按年份,X 轴网格线按财政季度

    我试图将 x 轴刻度标签设置为年份 但将网格线设置为财政季度 数据很简单 只是一个groupby date count 见下文 每个日期都有一个计数 我将其绘制为线图 rc rc form Bakken rc tgt oil groupby
  • 什么时候在Python中使用弱引用?

    谁能解释一下弱引用的用法吗 The 文档 http docs python org library weakref html没有具体解释 只是说GC可以随时销毁通过弱引用链接到的对象 那么拥有一个随时可能消失的物体还有什么意义呢 如果我需要
  • Python Flask 删除请求

    我正在开发一个 Python 应用程序并使用 Flask 这是我的 DELETE 函数 app route DeleteMessage methods DELETE def DeleteMessage messages Message qu
  • Python TypeError:不支持的操作数类型 -:“int”和“function”

    我是 Python 初学者 正在做一项作业 我不断得到TypeError unsupported operand type s for int and function 即使在研究了错误并应用了建议的修复之后 我并不是在寻找任何人给我一个解
  • 从 java 代码运行 Python 脚本

    这是我第一次在java中尝试python 我正在尝试从我的代码执行 python 脚本 如下所示 Process process Runtime getRuntime exec python C Users username Desktop
  • 映射 2 个数据帧并替换目标数据帧中匹配值的标头

    我有一个数据框 df1 SAP Name SAP Class SAP Sec Avi 5 C Rison 6 A Slesh 7 B San 8 C Sud 7 B df2 Name Fi Class Avi 5 Rison 6 Slesh
  • Python Jinja2 调用宏会导致(不需要的)换行符

    我的 JINJA2 模板如下所示 macro print if john name if name John Hi John endif endmacro Hello World print if john Foo print if joh
  • 如何向 Jupyter (ipython) 笔记本自动添加扩展?

    我已经安装了扩展 calico document tools 我可以使用以下命令从 Jupyter 笔记本中加载它 javascript IPython load extensions calico document tools 如何为每个
  • 如何在我的 GUI 上绘图

    我正在设计一个 GUIPyQt当我单击一个按钮来绘制我创建的函数的数据图时 我需要显示一个 matplotlib pylab 窗口 它就像 Matlab 中使用的运行时 每次按下该按钮时 我都想将 matplotlib pylab 窗口保留
  • 在Python中将月份和年份的列合并为季度和年份的列

    我有一个数据框 df Month 1 8 Year 2015 2020 df pd DataFrame data df df 想要将其转变为新列 期望的输出 df Month 1 8 Year 2015 2020 Quarter Q1201
  • 折叠 numpy 数组除前两个维度之外的所有维度

    我有一个可变维度的 numpy 数组 例如它可以具有以下形状 64 64 64 64 2 5 64 64 40 64 64 10 20 4 我想要做的是 如果维数大于 3 我想将其他所有内容折叠 堆叠到第三维中 同时保留顺序 因此 在我上面
  • pip 升级到 pip 10.x.x 后解析需求文件的正确方法?

    所以今天我确实发现随着发布pip 10 x x the req软件包更改了其目录 现在可以在下面找到pip internal req 由于通常的做法是使用parse requirements功能在你的setup py从需求文件中安装所有依赖
  • Python for 循环前瞻

    我有一个 python for 循环 其中我需要向前查看一项以查看在处理之前是否需要执行某项操作 for line in file if the start of the next line 0 perform pre processing
  • 如何让 Python 生成器返回 None 而不是 StopIteration?

    我使用生成器在列表中执行搜索 如下简单示例 gt gt gt a 1 2 3 4 gt gt gt i for i v in enumerate a if v 4 next 3 只是为了稍微说明一下示例 与上面的列表相比 我使用的列表要长得
  • ValueError:序列太大;不能大于 32

    我写了这段代码 from Crypto Cipher import AES import numpy as np import cv2 base64 BLOCK SIZE 16 PADDING pad lambda s s BLOCK SI
  • pandas groupby 中两个系列的最大值和最小值

    是否可以从 groupby 中的两个系列中获取最小值和最大值 例如下面的情况 分组时c 我怎样才能得到最小值和最大值a and b同时 df pd DataFrame a 10 20 3 40 55 b 5 14 8 50 60 c x x
  • Maya python 连接选择的属性

    我一直在尝试制作一个简单的脚本 它将采用两个视口选择 然后基本上将第二个视口的旋转连接到第一个 我不确定如何正确地从视口选择中为对象创建变量 这是我的尝试 但不起作用 import maya cmds as cmds sel cmds ls
  • VSCode IntelliSense 认为 Python 'function()' 类存在

    VSCode IntelliSense 正在完成一个名为的 Python 类function 这似乎不存在 例如 这似乎是有效的代码 def foo value return function value foo 0 But functio
  • print() 函数的有趣/奇怪的机制

    我正在学习Python 我目前正在学习如何定义自己的函数 并且在尝试理解返回值和打印它之间的区别时遇到了一些困难 我读到的关于这个主题的描述对我来说不太清楚 所以我开始自己尝试 我想我现在已经明白了 如果我没记错的话 区别在于你可以传递 a
  • 合并共享属性的节点

    EDITED 我真的需要 Networkx graph 专家的帮助 假设我有以下数据框 我想将这些数据框转换为图表 然后我想根据描述和优先级属性将两个图映射到相应的节点 df1 From description To priority 10

随机推荐