I want to save all my email messages from the inbox folder of outlook using Python. I can save the First or the last messages but couldn't understand how to get all the messages of the folder. Hence, I was trying to use a loop to iterate the code for all mails. I have tried to use the following code:
from win32com.client import Dispatch
import os
import re
os.chdir("D:\\emails")
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
print(inbox)
messages = inbox.items
message = messages.GetLast()
name = str(message.subject)
name = re.sub('[^A-Za-z0-9]+', '', name)+'.msg'
for message in messages:
message.SaveAs(os.getcwd()+'//'+name)
This is not giving me any errors, but saving only the last mail, instead of saving all emails. Could you please help me to rectify the code. Thanks.