Forward emails from a specifc sender in outlook via python - python

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!

Quick Example
import win32com.client
def outlook_emails(Inbox):
Filter_sender = "#SQL=""urn:schemas:httpmail:fromemail"" " \
"ci_phrasematch 'TDC#AE.Roco.COM'"
items = Inbox.Items.Restrict(Filter_sender)
print(items.Count)
for i in reversed(range(items.Count, 0, -1)):
if items[i].Class == 43:
print(items[i].Subject)
if __name__ == "__main__":
outlook = win32com.client.Dispatch(
"outlook.Application").GetNamespace(
"MAPI"
)
inbox = outlook.GetDefaultFolder(6)
outlook_emails(inbox)
#SQL=""urn:schemas:httpmail:fromemail"" ci_phrasematch

Not all Outlook items provide the Sender property. Outlook folders may contains different kind of items - notes, calendar items, mail items, document items and etc. So, it makes sense to make sure that you deal with a mail item, for example, you could check the MessageClass property.
Instead of iterating over all items in Outlook:
for message in messages:
if message.Sender.Address == sender_email:
Use the Find/FindNext or Restrict methods of the Items class. They allow getting items that correspond to the search criteria, so you can iterate over them without checking a property each time. Read more about these methods in the articles that I wrote for the technical blog:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
Use the following search criteria to get items from the specified email address:
criteria = "#SQL=" & Chr(34) _
& "urn:schemas:httpmail:senderemail" & Chr(34) _
& " = 'eugene#astafiev.com'"
If you need to send an item from a specific sender, you can use the MailItem.SendUsingAccount property returns or sets an Account object that represents the account under which the MailItem is to be sent. Note, the other account should be configured in Outlook. For example, a sample in VBA shows how to use it:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
Another approach is to use the MailItem.SentOnBehalfOfName property which returns or sets a string indicating the display name for the intended sender of the mail message. In that case the other user should have permissions to send on behalf of another person in Exchange.

Sender property is only exposed by the MailItem object, but you can also have ReportItem and MeetingItem objects in the Inbox folder. You need to check first that Class property == 43 (which is olMail)
Also, do not loop through all items in a folder - use Items.Find/FindNext or Items.Restrict with a query like [SenderEmailAddress] = 'TDC#AE.Roco.COM'

Related

Python to Forward specific mails with attachments recieved today in outlook

I'm trying to figure out a way to forward emails with attachments received today
from " invoice#email.com" to "Remas#email.com".
I am stuck in that line, What to put here exactly to let it know that any attachment received from invoice#email.com" today, send it to "Remas#email.com" automatically.
What to type here ?
messages = inbox.Items(????????)
My code:
import win32com.client
import time
import datetime as dt
from os import path
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
for message in messages:
NewMsg = message.Forward()
NewMsg.Body = message.Body
NewMsg.Subject = message.Subject
NewMsg.To = "Remas#email.com"
#NewMsg.Send()
Instead of iterating over all items in the Outlook folder:
for message in messages:
NewMsg = message.Forward()
You need to use the Find/FindNext or Restrict methods of the Items class. They allow getting items that correspond to your conditions and iterate over them only. You may get items recieved today by using the following search criteria:
strFilter = "%today("urn:schemas:httpmail:datereceived")%"
To get items for a specific time frame (for example, several days or hours) you may combine conditions using logical operators:
'This filter uses urn:schemas:httpmail namespace
strFilter = ""urn:schemas:httpmail:datereceived"" > '{datStartUTC}' AND ""urn:schemas:httpmail:datereceived"" < '{datEndUTC}'"
Read more about filtering items using a date-time comparisons in the Filtering Items Using a Date-time Comparison article.
Also you may get items that has any files attached, so you don't need to iterate over all items found for a specific date range:
" "urn:schemas:httpmail:hasattachment"=1 "
Read more about the Find/Find and Restrict methods in the articles that I wrote for the technical blog:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
If you need to process multiple folders at once you may consider using the AdvancedSearch method. See Advanced search in Outlook programmatically: C#, VB.NET for more information.
Work with Items.Restrict method (Outlook) to apply a filter to the inbox.Items collection
Example
import win32com.client
import time
import datetime as dt
from os import path
outlook = win32com.client.Dispatch(
"Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
today = str(dt.date.today())
filter_message = f"([ReceivedTime] > '{today} 00:00 AM') AND " \
f"([SenderEmailAddress] = 'invoice#email.com')"
for message in messages.Restrict(filter_message):
# if message has attachments
if message.Attachments.Count > 0:
NewMsg = message.Forward()
NewMsg.HTMLBody = message.HTMLBody
NewMsg.Subject = message.Subject
NewMsg.To = "Remas#email.com"
# NewMsg.Send()
NewMsg.Display()
You may also wanna check MailItem.SenderEmailType property (Outlook)
Returns a String that represents the type of entry for the email address of the sender of the Outlook item, such as 'SMTP' for Internet address, 'EX' for a Microsoft Exchange server address, etc. Read-only.
Example
https://stackoverflow.com/a/74816451/4539709

how to use win32com.mapi get new email from outlook in python?

I wanna get a message when a new email into outlook.
I try to use python.
I can find that folders,but can't find the way to get the number of new email.
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts = win32com.client.Dispatch("Outlook.Application").Session.Accounts;
inbox = outlook.Folders(accounts[0].DeliveryStore.DisplayName)
for obj in inbox.Folders: #how to know how many new email in this dir?
try:
if hasattr(obj, "__str__"):
dirName = obj.__str__() #as some new email in this obj.
for message in obj.items: # how to know this email is not be read ?
subject = sender = ''
if hasattr(message, "Subject"):
subject = message.Subject
if hasattr(message, "SenderName"):
sender = message.SenderName
print(sender, subject)
except Exception as e:
print(f"i:{obj.__str__()}")
and where can I learn win32com.mapi?
I wanna know what func can I use in mapi.
give me some info,please~
so much thanks!
Are you looking for unread messages? Use MAPIFolder.Items.Restrict("[Unread] = true") to retrieve Items collection with the restriction applied.

How to save MS Outlook attachments from specific sender and date using Python

I am a bit new to coding and I am trying to understand how to get Python to save MS Outlook attachments from a specific sender. I currently receive the same email from the same person each day regarding data that I need to save to a specific folder. Below are the requirements I am trying to meet:
I want to open MS Outlook and search for specific sender
I want to make sure that the email that I am opening from the specific sender is the most current date
I want to save all attached files from this sender to a specific folder on my desktop
I have seen some posts on using win32com.client but have not had much luck getting it to work with MS Outlook. I will attach some code I have tried below. I appreciate any feedback!
import win32com.client
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages=inbox.Items
for message in messages:
attachments = message.attachments
for attachment in attachments:
pass
You almost got it, add filter to the sender email address
import win32com.client
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)
Filter = "[SenderEmailAddress] = '0m3r#email.com'"
Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()
for attachment in Item.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(r"C:\path\to\my\folder\Attachment.xlsx")
Python 3.8 on windows
def saveAttachments(email:object):
for attachedFile in email.Attachments: #iterate over the attachments
try:
filename = attachedFile.FileName
attachedFile.SaveAsFile("C:\\EmailAttachmentDump\\"+filename) #Filepath must exist already
except Exception as e:
print(e)
for mailItem in inbox.Items:
#Here you just need to bould your own conditions
if mailItem.Sender == "x" or mailItem.SenderName == "y":
saveAttachments(mailItem)
The actual conditions you can change to your liking. I would recommend referring to the Object model for Outlook MailItem objects: https://learn.microsoft.com/en-gb/office/vba/api/outlook.mailitem
Specifically its Properties

Python get only the most recent Outlook email from a conversation

I'm using Python 3.5 and iterating through Outlook emails searching by message subject, if a condition is met I Save the mail to desktop. I have a problem because as I iterate through mails, I end up getting all the mails in a conversation (Both original mail and all the responses RE:) when i print the subjects that met the if condition.
On the other hand, when i save them to desktop (message.SaveAs) i get only the first email in a conversation.
What i'm interested in is only the most recent mail from a conversation, because if I save that to desktop i also get all the previous responses, so no need for 30 .msg files. Is there a way to do it? Here's my code:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders("Main")
subfolder = folder.Folders("Incoming")
inbox = subfolder.Folders("folder1")
for x in IDX:
messages = inbox.Items
message = messages.GetFirst()
for _ in itertools.repeat(None, 100):
try:
Subject = message.subject
if x in Subject:
print(Subject)
message.SaveAs(desktop + '\\' + Subject + ".msg", OlSaveAsType['olMSG'])
message = messages.GetNext()
except:
message = messages.GetNext()
Firstly, do not loop through all items in a folder - call Items.Restrict() passing a restriction like " [Subject] = 'you subject' ", then sort the returned Items collection (Items.Sort) on ReceivedTime.

How to delete most recently mail sent in python?

Using python and imaplib, how can I delete the most recently sent mail?
I have this:
mail = imaplib.IMAP4_SSL('imap-mail.outlook.com')
mail.login('MYEMAIL#hotmail.com', 'MYPASS')
mail.select('Sent')
mail.search(None, "ALL") # Returns ('OK', ['1 2 ... N'])
# doubt
Thanks in advance!
You'll need to use the select method to open the appropriate folder with read and write permissions. If you do not want to mark your messages as seen, you need to use the examine method.
The sort command is available, but it is not guaranteed to be supported by the IMAP server. For example, Gmail does not support the SORT command.
To try the sort command, you would replace M.search(None, 'ALL') with M.sort(search_critera, 'UTF-8', 'ALL')
Then search_criteria would be a string like:
search_criteria = 'DATE' #Ascending, most recent email last
search_criteria = 'REVERSE DATE' #Descending, most recent email first
search_criteria = '[REVERSE] sort-key' #format for sorting
According to RFC5256 these are valid sort-key's:
"ARRIVAL" / "CC" / "DATE" / "FROM" / "SIZE" / "SUBJECT" / "TO"
I found a solution that worked for me. After get sent mailbox access I was needing to found a message with fetch() function and then delete the email message with expunge() function. From imaplib documentation:
IMAP4.expunge()
Permanently remove deleted items from selected
mailbox. Generates an EXPUNGE response for each deleted message.
Returned data contains a list of EXPUNGE message numbers in order
received.
My code:
mail = imaplib.IMAP4_SSL('imap-mail.outlook.com')
mail.login('MYEMAIL#hotmail.com', 'MYPASS')
mail.select('Sent')
typ, data = mail.search(None, 'ALL')
control = 0
tam = len(data[0].split())
while control < tam:
typ, data = mail.fetch(tam - control, '(RFC822)')
if str(data).find(msg['Subject']) and str(data).find(msg['To']) != -1:
print "Msg found! ", control + 1, "most recently message!"
mail.store(str(tam - control), '+FLAGS', '\\Deleted')
mail.expunge()
break
control = control + 1
mail.close()
mail.logout()

Categories