email.utils
.
for recipient in getaddresses(
chain(email.get_all('to', []), email.get_all('cc', []), email.get_all('bcc', []))
):
print('The recipient is: ', recipient)
the docs on getaddresses
:
此方法返回由返回的形式的2元组列表
parseaddr()。fieldvalues可能是标题字段值的序列
通过消息返回。全部获取。
get_all
会回来的
None
如果缺少标头,除非传入默认值,否则
get_all('to', [])
此消息的另一个优点是可以正确解析一些非常糟糕但完全有效的电子邮件地址:
msg = b"""
From: wayne@example.com
To: Person Man <person.man@example.com>, Fredrick Douglas <music.man@example.org>
Cc: Guido <bdfl@example.com>, FLUFL <barry@example.com> ,"Abc\@def"@example.com ,"Fred Bloggs"@example.com ,"Joe\\Blow"@example.com ,"Abc@def"@example.com ,customer/department=shipping@example.com ,\$A12345@example.com ,!def!xyz%abc@example.com ,_somename@example.com, much."more\ unusual"@example.com, very.unusual."@".unusual.com@example.com, very."(),:;<>[]".VERY."very@\\"very".unusual@strange.example.com
Subject: Testing email
This isn't a very fancy email, but I'm just trying to prove a point here, OK?
""".strip()
就这么说吧
,
very."(),:;<>[]".VERY."very@\\"very".unusual@strange.example.com
这是一个完全有效的电子邮件地址。