I have a code that loops through a list of mails, but it is only showing the first result, even though there are also other matches.
The other results require me to loop over the mails again only to return the value.
mail = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
inbox = mail.GetDefaultFolder(6).Folders["Trial"]
df = pd.read_excel(r"C://User//Asus//Test//Test.xlsx", sheet_name = 'Sheet1')
mail = df.iloc[:,0].tolist() #this is the list of emails i.e. ends with gmail.com
processed = mail.GetDefaultFolder(6).Folders["Trial"].Folders["Done"]
msg = inbox.Items.GetFirst()
add = msg.SenderEmailAddressfor attch in msg.Attachments:if any([add.endswith(m) for m in mail]) and attch.FileName[-3:] != 'png':fname = attch.FileNamedir = "C://User//Asus//Output//"tempdir = os.path.join(dir, fname)attch.SaveAsFile(tempdir)
msg.Move(processed) #move the emails that completed processed to another folder
Updated Codes V1
I had revised the codes according to the suggestion, but with bad luck, now it only loops through the first two email..
mail = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
inbox = mail.GetDefaultFolder(6).Folders["Trial"]
df = pd.read_excel(r"C://User//Asus//Test//Test.xlsx", sheet_name = 'Sheet1')
mail = df.iloc[:,0].tolist() #this is the list of emails i.e. ends with gmail.com
processed = mail.GetDefaultFolder(6).Folders["Trial"].Folders["Done"]
add = msg.SenderEmailAddressfor msg in inbox.Items:print(msg.Subject)for attch in msg.Attachments:if any([add.endswith(m) for m in mail]) and attch.FileName[-3:] != 'png':fname = attch.FileNamedir = "C://User//Asus//Output//"tempdir = os.path.join(dir, fname)attch.SaveAsFile(tempdir)msg.Move(processed) #move the emails that completed processed to another folder
Updated Codes V2
for msg in inbox.Items:try:for attch in msg.Attachments:if msg.SenderEmailType == "SMTP" and attch.FileName[-3:] != 'png':add = msg.SenderEmailAddressprint(add, "address")fname = attch.FileNamedir = "C://User//Asus//Output//"tempdir = os.path.join(dir, fname)attch.SaveAsFile(tempdir)msg.Move(processed)elif msg.SenderEmailType == "EX" and attch.FileName[-3:] != 'png':add = msg.Sender.GetExchangeUser().PrimarySmtpAddressprint(add, "address")fname = attch.FileNamedir = "C://User//Asus//Output//"tempdir = os.path.join(dir, fname)attch.SaveAsFile(tempdir)msg.Move(processed)except Exception as e:print(e)continue