Issue
How to read email file (saved email to local drive, with “.msg” extension)?
I tried this 2 lines and it doesn't work out.
msg = open('Departure HOUSTON EXPRESS Port NORFOLK.msg', 'r')
print msg.read()
I searched the web for an answer, which gave the below code:
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:
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 it’d better to convert the .msg to .eml before parsing but I am not really sure how.
What would be the best way to read a .msg file?
Solution
The code you have now looks to be completely unworkable for what you're trying to accomplish. You need to parse Outlook ".msg" files, which can be done in Python but not using the email
module. But if you can use ".eml" files as you mentioned, it will be easier because the email
module can read those.
To read .eml files, see email.message_from_file().
Answered By - John Zwinck
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.