Saving email from Outlook into folder with Python

13,167

Solution 1

The below code works:

from win32com.client import Dispatch
import os
import re

outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
print(inbox)
messages = inbox.items
message = messages.GetLast()
name = str(message.subject)
#to eliminate any special charecters in the name
name = re.sub('[^A-Za-z0-9]+', '', name)+'.msg'
#to save in the current working directory
message.SaveAs(os.getcwd()+'//'+name)

Solution 2

SaveAsFile is a method only to be used on attachments.

For the message itself, use simply message.SaveAs().

Source: https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/mailitem-saveas-method-outlook

Share:
13,167

Related videos on Youtube

pypi34
Author by

pypi34

Updated on June 04, 2022

Comments

  • pypi34
    pypi34 almost 2 years

    I'm trying to automate saving emails in our folders from outlook. I don't see a code for saving an email as .msg or any other type.

    import win32com.client
    import os
    os.chdir("filepathhere")
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts;
    
    Tokyo = "email from Tokyo"
    
    inbox = outlook.GetDefaultFolder(6) 
    subject = Tokyo
    messages = inbox.Items
    message = messages.GetFirst()
    
    for msg in messages:
        if msg.subject == Tokyo:
            msgname = msg.subject
            msgname=str(msgname)
            print msgname
            message.saveasfile(msgname+".msg")
    

    I get the error message: AttributeError: .saveasfile

    • GetHacked
      GetHacked almost 6 years
      If you want to save the message that you're on within the loop, your last line should read msg.saveasfile(). Does that help? I'm not familiar with the win32com package.
    • pypi34
      pypi34 almost 6 years
      I still get the error that says: AttributeError: <unknown>.saveasfile
    • abarnert
      abarnert almost 6 years
      Where did you get that saveasfile from? Are you just guessing at what methods the object might provide and crossing your fingers? Look up the type in the MSDN docs.
    • pypi34
      pypi34 almost 6 years
      saveasfile() is the method I've seen people use to save attachment from emails. I haven't been able to find the right command
    • abarnert
      abarnert almost 6 years
      First, where have you seen that? If you give us a link, we can check whether you're actually doing the same thing as them. Second, you're not trying to save an attachment, you're trying to save a message. Those are obviously going to be different types, so why are you assuming they'll have the same methods?
    • GetHacked
      GetHacked almost 6 years
    • pypi34
      pypi34 almost 6 years
      it says i don't currently have the right permissions to access to folder I'm trying to save in (separate issue) - but this seems to be working! my last line was msg.SaveAs(file_path) Thanks!
    • GetHacked
      GetHacked almost 6 years
      @pypi34 No problem! I added it as an answer so as not to break the "answering in comments" rule.