我想找到最清晰的方法来解析邮件头。你知道吗
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\u 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
标头通过其addresses属性公开地址详细信息。你知道吗
鉴于此信息:
地址如下所示:
可通过索引访问各个地址:
解析器似乎要处理格式错误的头:
相关问题 更多 >
编程相关推荐