pythonparsingsmtprfc822

Mail Internet header analyzer in Python?


been going through old posts but so far only found solutions for identifying e.g. sender, recipient, subject. I'm looking to get started on code that would analyze the internet header similar to tools like https://testconnectivity.microsoft.com/MHA/Pages/mha.aspx and https://toolbox.googleapps.com/apps/messageheader/ .

I would like to be able to extract e.g. From, Reply-to, submitting MX, X-originating IP, X-mailer. Should I create a parser from scratch or is there something I could use? Perhaps a sample or something you can share?

Best, Fredrik


Solution

  • email module deals with e-mails quite nicely. For example:

    import email
    
    msg = email.message_from_file("some_saved_email.eml")
    # To get to headers, you treat the Message() as a dict:
    print msg.keys() # To get all headers
    print msg["X-Mailer"] # To get to header's value
    
    # Let us list the whole header:
    for header, value in msg.items():
        print header+": "+value