Skip to main content
 首页 » 编程设计

python之使用 Python 分解电子邮件地址

2024年11月24日2Terrylee

我正在尝试找到解析电子邮件 header 的最清晰方法。

Python 类 https://docs.python.org/3/library/email.message.html允许访问标题,例如电子邮件 ['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 

请您参考如下方法:

header 通过其 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>' 

解析器似乎可以处理格式错误的 header :

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