email - Parsing "From:" field of an e-mail message in Python -


i trying parse rfc 5322 compliant "from: " field in e-mail message 2 parts: display-name, , e-mail address, in python 2.7 (the display-name empty). familiar example

john smith <jsmith@example.org> 

in above, john smith display-name , jsmith@example.org email address. following valid "from: " field:

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

in example, return value display-name

"unusual"  

and

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

is email address.

you can use grammars parse in perl (as explained in these questions: using regular expression validate email address , the recognizing power of “modern” regexes), i'd in python 2.7. have tried using email.parser module in python, module seems able separate fields distinguished colon. so, if like

from email.parser import parser headers = parser().parsestr('from: "john smith" <jsmith@example.org>') print headers['from']  

it return

"john smith" <jsmith@example.com>  

while if replace last line in above code with

print headers['display-name'] 

it return

none 

i'll appreciate suggestions , comments.

headers['display-name'] not part of email.parser api.

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

it handles unusual address:

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

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -