Using Python to read saved “.msg” email file

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Mark K
Posts: 2
Joined: Mon Nov 10, 2014 9:03 am UTC

Using Python to read saved “.msg” email file

Postby Mark K » Mon Nov 10, 2014 9:08 am UTC

Hello all,


Can you please tell me how to read email file (saved email to local drive, with “.msg” extension)?

I tried this 2 lines and certainly it doesn’t work out.

Code: Select all

msg = open('Departure  HOUSTON EXPRESS  Port  NORFOLK.msg', 'r')
print msg.read()


googled out an answer giving below codes:

Code: Select all

import email
def read_MSG(file):
    email_File = open(file)
    messagedic = email.Message(email_File)
    content_type = messagedic["plain/text"]
    FROM = messagedic["From"]
    TO = messagedic.getaddr("To")
    sujet = messagedic["Subject"]
    email_File.close()
    return content_type, FROM, TO, sujet


myMSG= read_MSG(r"c:\\myemail.msg")
print myMSG


however it gives an error:

Code: Select all

Traceback (most recent call last):
File "C:\Python27\G.py", line 19, in <module>
myMSG= read_MSG(r"c:\\myemail.msg")
File "C:\Python27\G.py", line 10, in read_MSG
messagedic = email.Message(email_File)
TypeError: 'LazyImporter' object is not callable


Some responses on Internet tell there are some possibilites for example, it’d better to convert the .msg to other formats such as .eml before parsing but not really how.

What would be the best way to read a .msg file? Thanks.

User avatar
phlip
Restorer of Worlds
Posts: 7543
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Using Python to read saved “.msg” email file

Postby phlip » Tue Nov 11, 2014 10:57 pm UTC

.msg is the internal Outlook email format, right? While .eml would be your actual plain-text MIME email?

Easy way to check: if you open up the file in a text editor, do you get a bunch of binary gibberish? Or something that looks like an email, with From: and To: headers, followed by the email body?

Because the "email" module will only read that second kind, as that's the standard format used everywhere. The first kind is an internal Microsoft Office format that is much harder to read. You might be able to have some amount of success with a module like this... or, on the other hand, you might not, as these files are notoriously hard to handle.

As for the actual error you're getting... the email module is split up into a number of submodules. I think what you actually want is:

Code: Select all

import email.parser

with open("myemail.eml") as email_file:
    message = email.parser.Parser().parse(email_file)
See the documentation's examples for more detail.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
thoughtfully
Posts: 2244
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Using Python to read saved “.msg” email file

Postby thoughtfully » Wed Nov 12, 2014 12:41 pm UTC

If it's a Outlook file, you might find some help at the win32 mailing list.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

Mark K
Posts: 2
Joined: Mon Nov 10, 2014 9:03 am UTC

Re: Using Python to read saved “.msg” email file

Postby Mark K » Mon Nov 17, 2014 3:43 am UTC

thanks phlip and thoughtfully for the replies.


Return to “Coding”

Who is online

Users browsing this forum: Google [Bot] and 7 guests