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

时间:2018-11-29 15:37:52

标签: 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: 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?

我可以将其粘贴在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))

我希望看到类似的东西

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>'

相反,我得到了:

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>'

有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

到目前为止,我发现的最好方法是使用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是可能的标头字段值序列   由Message.get_all返回。

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

这是一个完全有效的电子邮件地址。