在Python中解析电子邮件的“From:”字段

2024-04-26 11:53:49 发布

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

我试图将电子邮件消息中符合RFC 5322的“发件人:”字段分为两部分:Python2.7中的显示名称和电子邮件地址(显示名称可以为空)。我们熟悉的例子是

John Smith <jsmith@example.org>

在上面,John Smith是显示名,并且jsmith@example.org是电子邮件地址。但以下也是有效的“发件人:”字段:

^{pr2}$

在本例中,display name的返回值为

"unusual" 

以及

"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com

是电子邮件地址。在

您可以在Perl中使用语法来解析它(如以下问题所述:Using a regular expression to validate an email addressThe recognizing power of “modern” regexes),但我希望在Python2.7中这样做。我试过用电子邮件.parser模块,但该模块似乎只能分隔用冒号区分的字段。所以,如果你像

from email.parser import Parser
headers = Parser().parsestr('From: "John Smith" <jsmith@example.org>')
print headers['from'] 

它会回来的

"John Smith" <jsmith@example.com> 

如果将上面代码中的最后一行替换为

print headers['display-name']

它会回来的

None

如有任何建议和意见,我将不胜感激。在


Tags: nameorg名称comexample电子邮件email地址
2条回答

headers['display-name']不是email.parserapi的一部分。在

试试看电子邮件.utils.parseaddr公司名称:

In [17]: email.utils.parseaddr("jsmith@example.com")
Out[17]: ('', 'jsmith@example.com')

In [18]: email.utils.parseaddr("(John Smith) jsmith@example.com")
Out[18]: ('John Smith', 'jsmith@example.com')

In [19]: email.utils.parseaddr("John Smith <jsmith@example.com>")
Out[19]: ('John Smith', 'jsmith@example.com')

它还处理您不寻常的地址:

^{pr2}$

我在C++中用libtld编写了这样的解析器。如果你真的想要完整的话,有lex和yacc(尽管我不使用这些工具)。我的C++ code可能会帮助您用python编写自己的版本。在

(lex part)
[-A-Za-z0-9!#$%&'*+/=?^_`{|}~]+                                          atom_text_repeat (ALPHA+DIGIT+some other characters)
([\x09\x0A\x0D\x20-\x27\x2A-\x5B\x5D-\x7E]|\\[\x09\x20-\x7E])+           comment_text_repeat
([\x33-\x5A\x5E-\x7E])+                                                  domain_text_repeat
([\x21\x23-\x5B\x5D-\x7E]|\\[\x09\x20-\x7E])+                            quoted_text_repeat
\x22                                                                     DQUOTE
[\x20\x09]*\x0D\x0A[\x20\x09]+                                           FWS
.                                                                        any other character

(lex definitions merged in more complex lex definitions)
[\x01-\x08\x0B\x0C\x0E-\x1F\x7F]                                         NO_WS_CTL
[()<>[\]:;@\\,.]                                                         specials
[\x01-\x09\x0B\x0C\x0E-\x7F]                                             text
\\[\x09\x20-\x7E]                                                        quoted_pair ('\\' text)
[A-Za-z]                                                                 ALPHA
[0-9]                                                                    DIGIT
[\x20\x09]                                                               WSP
\x20                                                                     SP
\x09                                                                     HTAB
\x0D\x0A                                                                 CRLF
\x0D                                                                     CR
\x0A                                                                     LF

(yacc part)
address_list: address
            | address ',' address_list
address: mailbox
       | group
mailbox_list: mailbox
            | mailbox ',' mailbox_list
mailbox: name_addr
       | addr_spec
group: display_name ':' mailbox_list ';' CFWS
     | display_name ':' CFWS ';' CFWS
name_addr: angle_addr
         | display_name angle_addr
display_name: phrase
angle_addr: CFWS '<' addr_spec '>' CFWS
addr_spec: local_part '@' domain
local_part: dot_atom
          | quoted_string
domain: dot_atom
      | domain_literal
domain_literal: CFWS '[' FWS domain_text_repeat FWS ']' CFWS
phrase: word
      | word phrase
word: atom
    | quoted_string
atom: CFWS atom_text_repeat CFWS
dot_atom: CFWS dot_atom_text CFWS
dot_atom_text: atom_text_repeat
             | atom_text_repeat '.' dot_atom_text
quoted_string: CFWS DQUOTE quoted_text_repeat DQUOTE CFWS
CFWS: <empty>
    | FWS comment
    | CFWS comment FWS
comment: '(' comment_content ')'
comment_content: comment_text_repeat
               | comment
               | ccontent ccontent

相关问题 更多 >