Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract just email headers in python

I'm having some issues trying to extract all the email headers in python. I know how to get the ones I'm looking for but I want to save all the headers and I'm not sure how to do that.

I have it loaded into a email object

import email
f = open(kwargs['opt_emailfile'])
msg = email.message_from_file(f)
f.close()

So I can get

msg['To']
msg['From']

But I want all the headers

Solution

Here is what I did thanks to the answer

        f = open(kwargs['opt_emailfile'])
        msg = email.message_from_file(f)
        f.close()

        parser = email.parser.HeaderParser()
        headers = parser.parsestr(msg.as_string())

        for h in headers.items():
            print h
like image 536
Mike Avatar asked Dec 07 '11 23:12

Mike


People also ask

How do I extract the contents of an email in Python?

To extract emails form text, we can take of regular expression. In the below example we take help of the regular expression package to define the pattern of an email ID and then use the findall() function to retrieve those text which match this pattern.

How do you print a header in Python?

columns to print column names in Python. We can use pandas. dataframe. columns variable to print the column tags or headers at ease.

Is there any header file in Python?

No, Python does not have header files nor similar. Neither does Java, despite your implication that it does. Instead, we use "docstrings" in Python to make it easier to find and use our interfaces (with the built-in help() function).


1 Answers

Using HeaderParser perhaps:

from email.parser import HeaderParser
parser = HeaderParser()
h = parser.parsestr(email)

print h.keys()

I just noticed you edited your question. You can actually get the same information from what you had without using HeaderParser. e.g. headers.items() will return list of 2-tuples with headers and corresponding values.

like image 59
jan zegan Avatar answered Sep 25 '22 02:09

jan zegan