Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29758

Reading attributes of .msg file

$
0
0

I am trying to read a .msg file to get the sender, recipients, and title.

I'm making this script for my workplace where I'm only allowed to install default python libraries so I want to use the email module to do this.

On the python website I found some examples of using the email module. https://docs.python.org/3/library/email.examples.html

Near the end of the page it talks about getting the sender, subject and recipient. I've tried using this code like this:

# Import the email modules we'll need
from email import policy
from email.parser import BytesParser

with open('test_email.msg', 'rb') as fp:
    msg = BytesParser(policy=policy.default).parse(fp)

# Now the header items can be accessed as a dictionary, and any non-ASCII will
# be converted to unicode:
print('To:', msg['to'])
print('From:', msg['from'])
print('Subject:', msg['subject'])

This results in an output:

To: None
From: None
Subject: None

I checked the file test_email.msg, it is a valid email.

When I add a line of code

print(msg) 

I get an output of a garbled email the same as if I opened the .msg file in notepad.

Can anybody suggest why the email module isn't finding the sender/recipient/subject correctly?


Viewing all articles
Browse latest Browse all 29758

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>