i try the below code but the attribute error is driving me crazy !, any thing after message. gets me an attribute error, wtv prop. i use. i end up with attribute error.
AttributeError: <unknown>.Sender
Code:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the inbox
messages = inbox.Items
sender_email = "TDC@AE.Roco.COM"
recipient_email = "simple.invoice@net"
for message in messages:
if message.Sender.Address == sender_email:
new_mail = message.Forward()
new_mail.Recipients.Add(recipient_email)
for attachment in message.Attachments:
new_mail.Attachments.Add(attachment)
new_mail.Save()
Based on given answers:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6)
accounts = mapi.Folders
query = '@SQL="urn:schemas:httpmail:from" = ' + "'TDC@AE.Roco.COM'" + ' AND "urn:schemas:httpmail:hasattachment" = ' + "'1'"
print(query)
try:
items = inbox.Items.Restrict(query)
print(f'Number of items found : {items.count}')
def check_subfolders(folder):
items = folder.Items.Restrict(query)
if items.count > 0:
print(f'{items.count} emails found in {folder.name}')
for subfolder in folder.Folders:
check_subfolders(subfolder)
check_subfolders(inbox)
for folder in mapi.Folders:
items = folder.Items.Restrict(query)
if items.count > 0:
print(f'{items.count} emails found in {folder.name}')
for item in items:
mail = item.Forward()
mail.Recipients.Add("simple.invoice@net")
mail.Subject = "Fwd: " + item.Subject
mail.Body = "Please find the forwarded message with attachments below:\n\n" + item.Body
mail.Save()
except Exception as e:
print(f'An error occurred: {e}')
Now I have no errors but the result returns zero, although I have mails from that specified sender!