用Python分解电子邮件地址

2024-09-27 23:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找到最清晰的方法来解析邮件头。你知道吗

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

Tags: to方法httpsorgdocsemailhtmllibrary
1条回答
网友
1楼 · 发布于 2024-09-27 23:18:27

标头通过其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'))
>>> 

相关问题 更多 >

    热门问题