蟒蛇的email
模块非常适合解析标头。但是,那To:
header可以有多个接收者,并且可能有多个To:
标头。那么如何拆分每个电子邮件地址呢?我不能用逗号分隔,因为逗号可以被引用。有没有办法做到这一点?
演示代码:
msg="""To: [email protected], "User Two" <[email protected]", "Three, User <[email protected]>
From: [email protected]
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: [email protected], "User Two" <[email protected]", "Three, User <[email protected]>
$
通过所有的To
线通过email.utils.getaddresses()
:
msg="""To: [email protected], John Doe <[email protected]>, "Public, John Q." <[email protected]>
From: [email protected]
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)