代码之家  ›  专栏  ›  技术社区  ›  Wayne Werner

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

  •  2
  • Wayne Werner  · 技术社区  · 7 年前

    假设我有一封电子邮件 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 回复  |  直到 7 年前
        1
  •  3
  •   Wayne Werner    7 年前

    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
    

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