I am completely new to I am trying to download attachments using MAPI based on date, sender's email address and subject line.
Below is my code:
import datetime
from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
folders = inbox.Folders
val_date = datetime.date.today().strftime("%d-%m-&y")
today = msg.Senton.date() = val_date
inboxtime = all_inbox and today
email_sender = 'Email input here'
sub_today = 'Subject line input here'
att_today = 'attachment name input here'
for msg in inboxtime:
if msg.SenderEmailType == "EX":
if msg.Sender.GetExchangeUser().PrimarySmtpAddress.find(email_sender) != -1
break
else:
if msg.SenderEmailAddress.find(email_sender) != -1 and msg.Senton.date() == val_date:
break
For att in msg.Attachments:
if att.FileName == att_today:
break
try:
att.SaveAsFile('D:\\' + att.FileName)
print(True)
except:
print(False)
However, it shows below error:
today= msg.Senton.date() == val_date
AttributeError: 'str' object has no attribute 'Senton'
Please help!
Firstly, this is not MAPI - this is Outlook Object Model. Secondly, you are assuming there are only MailItem
objects in the Inbox folder - but you can also have ReportItem
, MeetingItem
, etc. Make sure first that you really have a MailItem
object - check if the Class
property == 43 (olMailItem
)