提取电子邮件收到:标题与Python电子邮件包 [英] extracting email Received: headers with Python email package
问题描述
我想从邮件中提取最终 Received:email标题。我从 email.message_from_file()
返回的消息。
I'd like to extract the Final Received: email header from a message. I have the Message as returned from email.message_from_file()
.
使用消息.get()
或 Message.get_item()
方法不能保证我将获得的许多Received:标头中的哪一个。 Message.get_all()
返回全部,但不保证订单。有没有办法得到最后一个?
Using the Message.get()
or Message.get_item()
methods don't guarantee which of the many Received: headers I will get. Message.get_all()
returns them all, but doesn't guarantee an order. Is there a way to be guaranteed to get the last one?
推荐答案
收到:
标头是时间戳的:
Received: from lb-ex1.int.icgroup.com (localhost [127.0.0.1])
by lb-ex1.localdomain (Postfix) with ESMTP id D6BDB1E26393
for <hd1@example.com>; Fri, 12 Dec 2014 12:09:24 -0500 (EST)
所以,做 messageInstance.get_all()
和排序所得列表,但你认为合适,一个例子如何做到这一点:
So, do messageInstance.get_all()
and sort the resulting list however you see fit, an example of how to do this:
import email.utils
import operator
def sort_key(received_header):
received_date = email.utils.parsedate_tz(received_header)
return received_date
received_header_list.sort(key=sort_key)
如果不行,请留言,我会很乐意进一步研究。
If it doesn't work, do leave a comment and I'll be happy to look into it further.
这篇关于提取电子邮件收到:标题与Python电子邮件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!