Skip to main content
 首页 » 编程设计

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

2024年09月03日39Terrylee

假设我有一封电子邮件作为 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>' 

有更好的方法吗?

请您参考如下方法:

到目前为止,我发现的最佳方法涉及 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 :

This method returns a list of 2-tuples of the form returned by parseaddr(). fieldvalues is a sequence of header field values as might be returned by Message.get_all.

如果 header 不存在,

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 

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