[Tutor] Parsing email headers

Cameron Simpson cs at cskk.id.au
Sun Apr 26 19:24:03 EDT 2020


On 26Apr2020 16:13, jim <jf_byrnes at comcast.net> wrote:
>OS = linux Mint 18,xx
>
>This may be a little OT, as I am as interested in the process leading 
>up to parsing the header as I am in the results of parsing it.
>
>What I want to do is figure out where an email came from without 
>actually opening it. We all get possible malicious emails. Some are 
>obvious but some look pretty real. Many times the From line just says 
>"Google" or "Chase", etc.  I wrote a little bare bones script that 
>will print out the From:, Return-Path: and the Sender: names from the 
>header.

Python has a pretty full email parsing library. Trite example assuming 
you have the message in a file:

    import email
    with open(email_message_file) as f:
      message = email.Parser.Parser(f)

That gets you a Message object in "message", with ready access to the 
headers and many other facilities.

You're probably interesting in the Received: headers (any of which may 
be forged of course).

DL Neil has pointed you at the imap and pop libraries available if you 
want to write stuff to connect to your mailbox over the net.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list