簡體   English   中英

在Python中解析電子郵件消息的“發件人:”字段

[英]Parsing “From:” field of an e-mail message in Python

我試圖將電子郵件中符合RFC 5322的 “發件人:”字段解析為兩部分:顯示名稱和電子郵件地址,在Python 2.7中(顯示名稱可能為空)。 熟悉的例子是這樣的

John Smith <jsmith@example.org>

在上面,John Smith是顯示名稱,jsmith @ example.org是電子郵件地址。 但以下也是一個有效的“發件人:”字段:

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

在此示例中,display-name的返回值為

"unusual" 

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

是電子郵件地址。

您可以使用語法在Perl中解析它(如以下問題中所述: 使用正則表達式驗證電子郵件地址“現代” 正則表達式 的識別能力 ),但我想在Python 2.7中執行此操作。 我曾嘗試在Python中使用email.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

我非常感謝任何建議和意見。

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

試試email.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')

它還處理您的不尋常地址:

In [21]: email.utils.parseaddr('''"unusual" <"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>''')
Out[21]: ('unusual', '"very.(),:;<>[]".VERY."very@ "very".unusual"@strange.example.com')

我在C ++的libtld中編寫了這樣一個解析器。 如果你想真正完成,有lex和yacc(雖然我不使用這些工具)。 我的C ++代碼可以幫助您在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM