Home > Blockchain >  Save Outlook message as eml file with attachments using Python
Save Outlook message as eml file with attachments using Python

Time:01-11

I'm struggle with one script using Python. I'm trying to create a function that saves message with attachment as a .eml file. Attachment is a excel file that is stored in my local folder.

Right now i have this code that displays good template to send, but it doesn't save this message as a eml file. It is empty message

def save_mail():
    outlook=win32.Dispatch("Outlook.Application")
    olNs = outlook.GetNamespace("MAPI")
    mail=outlook.CreateItem(0)
    mail.To="[email protected]"
    mail.Subject="TEST TEST"
    mail.HTMLBody = "<p>TEST HTML</p>"
    attachment=(file_path)
    mail.Attachments.Add(attachment)
    mail.SaveAs(path_file   '.eml', 9)
save_mail()

I don't know how I should solve this problem. Please help :)

CodePudding user response:

The Outlook object model doesn't provide any property or method to save the item using the EML file format. EML, short for electronic mail or email, is a file extension for an email message saved to a file in the Internet Message Format protocol for electronic mail messages. It is the standard format used by Microsoft Outlook Express as well as some other email programs, but not Outlook. You may consider using third-party libraries such as Redemption for saving items using the EML file format.

CodePudding user response:

Outlook Object Model does not allow to export in the MIME (EML) format.

You can either build EML file from the scratch (it is a text file) one property at a time, or you can use a library like Redemption (I am its author) - its SafeMailItem and RDOMail objects support olRfc822 format in the SaveAs method in addition to the formats supported by Outlook.

  • Related