如何在Python中从RFC 2822邮件头中提取多个电子邮件地址?

10

Python的email模块非常适合解析邮件头。然而,To:头部可能有多个收件人,并且可能有多个To:头部。那么如何拆分每个电子邮件地址呢?我不能通过逗号分割,因为逗号可能被引用。有没有办法做到这一点?

演示代码:

msg="""To: user1@company1.com, "User Two" <user2@company2.com", "Three, User <user3@company3.com>                               
From: anotheruser@user.com                                                                                                      
Subject: This is a subject                                                                                                      

This is the message.                                                                                                            
"""

import email

msg822 = email.message_from_string(msg)
for to in msg822.get_all("To"):
    print("To:",to)

当前输出:

$ python x.py
To: user1@company1.com, "User Two" <user2@company2.com", "Three, User <user3@company3.com>
$ 

你想要什么输出? - Robᵩ
1
可能你应该选择 shlex & Co.。MDAs 通过(未引用的)逗号来分割地址行。 - user3159253
1
相关内容:https://docs.python.org/2/library/email.util.html#email.utils.getaddresses - Robᵩ
1个回答

9

将所有To行通过email.utils.getaddresses()处理:

msg="""To: user1@company1.com, John Doe <user2@example.com>, "Public, John Q." <user3@example.com>
From: anotheruser@user.com
Subject: This is a subject

This is the message.
"""

import email

msg822 = email.message_from_string(msg)
for to in email.utils.getaddresses(msg822.get_all("To", [])):
    print("To:",to)

请注意,我重新编写了您的To行。 我认为您的示例格式无效。
参考资料:https://docs.python.org/3/library/email.utils.html#email.utils.getaddresses

1
太好了。我阅读了文档,但是我找不到我要找的内容。谢谢! - vy32
Python 3的链接:https://docs.python.org/3/library/email.utils.html#email.utils.getaddresses - Neara
@Neara - 谢谢。已更新。 - Robᵩ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接