Skip to content

Instantly share code, notes, and snippets.

@GitterDoneScott
Created October 4, 2019 17:14
Show Gist options
  • Save GitterDoneScott/834764ccc67fa5a4c4b0acc10cd31f62 to your computer and use it in GitHub Desktop.
Save GitterDoneScott/834764ccc67fa5a4c4b0acc10cd31f62 to your computer and use it in GitHub Desktop.
Save (export) all messages in Outlook inbox as .msg files
from win32com.client import Dispatch
import re
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
for message in all_inbox:
try:
name = str(message.subject)
#to eliminate any special characters in the name
name = re.sub('[^A-Za-z0-9\s\-]+', '', name)+'.msg'
message.SaveAs('C:\\tmp\\attachments'+'\\'+name)
print("SUCCESSFUL","Message Saved")
except:
print("ERROR","Message failed to save")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment