save outlook attachments with python - python

Every Monday I receive the attachments with the slightly changed subject title. Constant part of the subject title is PB Report and will have the Monday's date. For example I received email this Monday with subject PB Report - 13.12.2021, last week PB Report - 06.12.2021 and so on. I would like to implement the GetLast in this code in order to get only the newest sent report. Also, how do I tell python to search the subject which starts with PB Report and dont look at the rest of the title. I tried wildcard(*) as save_attachments('PB Report*') but did not work.
import datetime
import os
import win32com.client
path = r"C:/Users/greencolor/Desktop/Autoreport/"
today = datetime.date.today()
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
def save_attachments(subject):
for message in messages:
if message.Subject == subject:
for attachment in message.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(path, str(attachment)))
if __name__ == "__main__":
save_attachments('PB Report - 13.12.2021')
I also have the alternative code but when i run this code i never get the result nor error. It takes.
import os
import win32com.client
path = r"C:/Users/greencolor/Desktop/Autoreport/"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetLast()
while message:
if 'PB' in message.subject and 'Report' in message.subject:
for attachment in message.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(path, str(attachment)))

The wildcard for parameter subject wont work because parameter subject is used as string when comparing for equality in message.Subject == subject.
You could instead use string-method startswith on the message subject like message.Subject.startswith(subject_prefix)
and call your method with a common prefix like save_attachments('PB Report - ').
Furthermore use the attachment.FileName to construct the output-file path.
You could use GetLast() or Sort() with appropriate message property to filter on the newest sent report. Or you could parse the dates in message's subject and sort them accordingly. However this would deserve another question, own research and further specification and focus.
Solution
An example solution could be as follows (see comments for adjustments):
def save_attachments(subject_prefix): # changed parameter name
messages.Sort("[ReceivedTime]", True) # sort by received date: newest to oldest
for message in messages:
if message.Subject.startswith(subject_prefix): # changed test
print("saving attachments for:", message.Subject)
for attachment in message.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(path, str(attachment.FileName))) # changed to file-name
return # exit after first matched message
Alternatively use GetLast() if you know the messages only contain desired ones, sorted by date.
message = messages.GetLast()
while message: # to test if there is a (last) message at all
# save attachment
Related questions
str.startswith with a list of strings to test for
Search the entire Outlook email box for specific emails using Python
How to go through Outlook emails in reverse order using Python

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 send an e-mail if a particular word is present in subject using Python

I have sent an automated e-mail using python with the following subject: "Daily Test Results are completed and please find the Test Results". Once this is done, I should wait and search for the reply for this email with the following subject: "Daily Test Results are completed and please find the Test Results - CHECK OK/VERIFIED/SEND/PASS_SEND"
Once I receive the mail with the new subject, then through python I must share the original mail with the first subject to multiple users.
I am not sure on two points:
For subject ending with CHECK OK/VERIFIED/SEND/PASS_SEND, I am not able to give a list and instead give only one keyword
If I give one keyword and search, I am able to get the mail, but I am not sure how to share the original mail to remaining participants
Can some one please help me with this
Code:
import win32com.client as win32
from datetime import date
def mail_check():
Outlook = win32.Dispatch('Outlook.Application').GetNameSpace('MAPI')
inbox = Outlook.GetDefaultFolder(6)
sub = [message for message in inbox.Items if 'CHECK OK' in message.Subject]
for mssg in sub:
print(mssg)
if __name__ == '__main__':
mail_check()

Get all emails from outlook inbox using Python and save them into a local folder

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.
The problem is below
message = messages.GetLast()
name = str(message.subject)
and
message.SaveAs(os.getcwd()+'//'+name)
You calculate the name once (before the loop) and use it in the loop.
You need to calculate the name during the loop.
The full code would look something like this.... (I got this answer from balderman)
from win32com.client import Dispatch
import os
import re
os.chdir("D:\\email")
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.items
for message in messages:
message = messages.GetNext()
name = str(message.subject)
name = re.sub('[^A-Za-z0-9]+', '', name)+'.msg'
message.SaveAs(os.getcwd()+'//'+name)

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

Check whether the email is a reply or response with Python win32com

I am using Python win32com to parse email from outlook . I am able to fetch email from the outlook folder , but I not able to verify whether the email is a reply or response or a forwarded message , I need to check whether the email reiceved is the reply of the previous mail (if yes then find the original mail) or email is the forwarded message. I am using following code to fetch emails from outlook.
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.Folders['xyz#xyz.com'].Folders['Inbox'].Folders['abc']
messagesReach = inbox.Items
for message in messagesReach:
if message.Unread==True:
print(message.body)
Hi the header is ConversationID and can be used as message.ConversationID
refer https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mailitem_properties.aspx
You could try to read the first three characters of the subject, and determine if it has the "Re:"-prefix and therefore is a reply. This should be the case most times.
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders["xyz#xyz.com"].Folders["Inbox"].Folders["abc"]
messagesReach = inbox.Items
for message in messagesReach:
if message.Unread == True:
if message.Subject[:3] == "Re:":
print(message.body)

Categories