2

Is it possible to download emails from outlook as .msg WITH attachments using Python?

I’ve gone down the rabbit hole and I’m pretty sure I can’t but wanted to ask here. I need them in one file and not two separate files.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Wizkidbrz
  • 21
  • 2

1 Answers1

0

You can save .msg file with attachments separately. You can use the win32com library to automate Outlook from Python.

The MailItem.SaveAs method saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used.

The Attachment.SaveAsFile method saves the attachment to the specified path.

For example, here is a basic sample code which iterates over all items in the folder (and attachments if any):

from win32com.client import Dispatch

outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items

for msg in all_inbox:
   for att in msg.Attachments:
      if att.FileName == "myfile.docx":
          break
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45