如何解决如何从python的RFC 2822邮件标题中提取多个电子邮件地址?
通过所有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
解决方法
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 <user3@company3.com>
$
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。