如何使用Python获取电子邮件的收件人?

如何使用Python获取电子邮件的收件人?,python,email,Python,Email,假设我有一封电子邮件,名为sample.eml,我想获得该电子邮件上所有收件人的列表。假设它看起来像这样: 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> Bcc: spani

假设我有一封电子邮件,名为
sample.eml
,我想获得该电子邮件上所有收件人的列表。假设它看起来像这样:

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>
Bcc: spanish.inquisition@example.com, The Dude <big.lebowski@example.net>
Subject: Testing email

This isn't a very fancy email, but I'm just trying to prove a point here, OK?
来自:wayne@example.com
致:此人,弗雷德里克·道格拉斯
抄送:Guido,FLUFL
密件抄送:西班牙语。inquisition@example.com那家伙
主题:测试电子邮件
这不是一封很花哨的电子邮件,但我只是想证明一点,好吗?
我可以将其粘贴到Python脚本中并解析电子邮件:

from email.parser import BytesParser
from itertools import chain

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>
Bcc: spanish.inquisition@example.com, The Dude <big.lebowski@example.net>
Subject: Testing email

This isn't a very fancy email, but I'm just trying to prove a point here, OK?
'''.strip()
email = BytesParser().parsebytes(msg)

for recipient in chain(email.get_all('to'), email.get_all('cc'), email.get_all('bcc')):
    print('Recipient is:', repr(recipient))
从email.parser导入BytesParser
来自itertools进口链
msg=b''
发件人:wayne@example.com
致:此人,弗雷德里克·道格拉斯
抄送:Guido,FLUFL
密件抄送:西班牙语。inquisition@example.com那家伙
主题:测试电子邮件
这不是一封很花哨的电子邮件,但我只是想证明一点,好吗?
''.strip()
email=BytesParser().parsebytes(msg)
对于链中的收件人(email.get_all('to')、email.get_all('cc')、email.get_all('bcc')):
打印('收件人为:',报告人(收件人))
我希望看到这样的情况:

Recipient is: 'Person Man <person.man@example.com>'
Recipient is: 'Fredrick Douglas <music.man@example.org>'
Recipient is: 'Guido <bdfl@example.com>'
Recipient is: 'FLUFL <barry@example.com>'
Recipient is: 'spanish.inquisition@example.com'
Recipient is: 'The Dude <big.lebowski@example.net>'
收件人是:'Person-Man'
收件人是:“弗雷德里克·道格拉斯”
收件人是:“Guido”
收件人是:“FLUFL”
收件人是:西班牙语。inquisition@example.com'
收件人是:“那个家伙”
相反,我得到的是:

Recipient is: 'Person Man <person.man@example.com>, Fredrick Douglas <music.man@example.org>'
Recipient is: 'Guido <bdfl@example.com>, FLUFL <barry@example.com>'
Recipient is: 'spanish.inquisition@example.com, The Dude <big.lebowski@example.net>'
收件人是:“个人,弗雷德里克·道格拉斯”
收件人是:“Guido,FLUFL”
收件人是:西班牙语。inquisition@example.com那家伙的

有更好的方法吗?

到目前为止,我发现最好的方法是
email.utils

for recipient in getaddresses(
    chain(email.get_all('to', []), email.get_all('cc', []), email.get_all('bcc', []))
):
    print('The recipient is: ', recipient)
发件人:

此方法返回由返回的形式的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