我试图找到最清晰的解析电子邮件标题的方法。
Python的类https://docs.python.org/3/library/email.message.html允许访问标头,例如email ['To']。
这是_UniqueAddressHeader类型,在https://docs.python.org/3/library/email.headerregistry.html中声明。
email ['To']似乎没有公开的方法,并且总是以复合字符串的形式返回。
我尝试使用以下方式明确创建地址类型
mailTo = email.headerregistry.Address(mail['To'])
但是这也不能正确组成对象-所有字符都被包含在'display_name'属性中,这不是我们所需要的。
编辑:这是我自己的功能,可能可以使其更健壮,以处理诸如<
>
等不匹配之类的错误
def addressSplit(e):
"""
:param e: email.header
:return: displayName, localpart, domainpart str
"""
s = str(e)
displayName = ''
openB = s.find('<')
closeB = s.find('>')
if openB>=0 and closeB>=0:
displayName = s[:openB].strip(' ')
s = s[openB+1:closeB].strip(' ') # this is the address part
localpart, domainpart = s.split('@')
return displayName, localpart, domainpart
答案 0 :(得分:0)
标头通过其addresses属性公开地址详细信息。
给出此消息:
>>> from email.message import EmailMessage
>>> from email.headerregistry import Address
>>> msg = EmailMessage()
>>> msg['to'] = [Address('Jane Smith', 'jane.smith', 'example.com'), Address('John Smith', 'john.smith', 'example.com')]
>>> print(msg)
to: Jane Smith <jane.smith@example.com>, John Smith <john.smith@example.com>
可以看到这样的地址:
>>> to = msg['to']
>>> to
'Jane Smith <jane.smith@example.com>, John Smith <john.smith@example.com>'
>>> type(to)
<class 'email.headerregistry._UniqueAddressHeader'>
>>> to.addresses
(Address(display_name='Jane Smith', username='jane.smith', domain='example.com'), Address(display_name='John Smith', username='john.smith', domain='example.com'))
可以通过索引访问个人地址:
>>> jane = to.addresses[0]
>>> jane.display_name
'Jane Smith'
>>> jane.username
'jane.smith'
>>> jane.domain
'example.com'
>>> jane.
jane.addr_spec jane.display_name jane.domain jane.username
>>> jane.addr_spec
'jane.smith@example.com'
>>> str(jane)
'Jane Smith <jane.smith@example.com>'
解析器似乎可以处理格式错误的标头:
>>> from email.parser import Parser
>>> from email.policy import default
>>> # Malformed address (missing '>')
>>> s = 'to: Jane Smith <jane.smith@example.com, John Smith <john.smith@example.com>'
>>> p = Parser(policy=default)
>>> msg = p.parsestr(s)
>>> to = msg['to']
>>> to.addresses
(Address(display_name='Jane Smith', username='jane.smith', domain='example.com'), Address(display_name='John Smith', username='john.smith', domain='example.com'))
>>>