Wednesday, 1 October 2014

How to read email message from .msg file

For reading the .msg file, I am using python built in library "email". you can read .msg file by 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("client.msg")
print myMSG

Thank you guys for your support.

4 comments:

  1. Traceback (most recent call last):
    File "C:/Python/Programs/msgExtracter/msg_extracter.py", line 11, in
    myMSG= read_MSG("TermSheet.msg")
    File "C:/Python/Programs/msgExtracter/msg_extracter.py", line 4, in read_MSG
    messagedic = email.Message(email_File)
    AttributeError: module 'email' has no attribute 'Message'

    ReplyDelete
    Replies
    1. It looks like the email module has been refactored over time. This fixed the 'LazyImporter' object not callable error for me on Python 2.7.


      under prior email versions it would have raised the exception

      TypeError: 'module' object is not callable

      You need to do like

      a = email.Message.Message()

      or per the new PEP 8 conformant naming

      a = email.message.Message()

      Delete
  2. AttributeError: module 'email' has no attribute 'message'

    ReplyDelete