如何从 python 中的 RFC 2822 邮件 header 中提取多个电子邮件地址?
How do you extract multiple email addresses from an RFC 2822 mail header in python?
Python 的 email
模块非常适合解析 header。但是,To:
header可以有多个收件人,也可以有多个To:
header。那么如何拆分每个电子邮件地址呢?我不能在逗号上拆分,因为可以引用逗号。有办法吗?
演示代码:
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>
$
将所有 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
模块非常适合解析 header。但是,To:
header可以有多个收件人,也可以有多个To:
header。那么如何拆分每个电子邮件地址呢?我不能在逗号上拆分,因为可以引用逗号。有办法吗?
演示代码:
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>
$
将所有 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