简体   繁体   中英

Loop through outlook mails with python

I receive a daily mail with the subject “XYZ” containing a CSV file with some information. I also got this python code which goes into my outlook account, looks for the mail with the subject “XYZ” and extracts the attachment and finally extends a certain database with the new information from the attachment. However, this code only gets the most recent email so that I need to run that code daily to keep it updated. If I'm not able to do that daily, lets say because I am on vacation, my database is going to miss some information from previous days.



outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = date.date.today()


sub_today = 'XYZ'
att_today = 'XYZ.csv'

"""
Look for the email with the given subject and the attachment name
"""
for msg in all_inbox:
    if msg.Subject == sub_today:
              break

for att in msg.Attachments:
    if att.FileName == att_today:
              break

"""
save the update and extend the database
"""
          
att.SaveAsFile('U:/' + 'XYZ.csv')
read_path = "U:/"
write_path = "V:/Special_Path/"


df_Update = pd.read_csv(read_path + 'XYZ.csv',parse_dates=['Date'],skiprows=2)
del df_Update['Unnamed: 14']

df_DB_Old = pd.read_csv(write_path + 'DataBase_XYZ.csv',parse_dates=['Date'])

DB_DatumMax = df_DB_Old['Date'].max()
Upd_DatumMax = df_Update['Date'].max()

if df_DB_Old['Date'].isin(pd.Series(Upd_DatumMax)).sum()>0:
    print(' ')
    print('Date already exists!')
    print(' ')
    input("press enter to continue...")
#    exit()
    sys.exit()
else:
    df_DB_New = pd.concat([df_DB_Old, df_Update])
    df_DB_New.to_csv(write_path + 'XYZ.csv',index=False)

Now I would like to extend that code, so that it checks when the last time was the database was updated and then it should extract the information from all the emails with subject “XYZ” starting from the day it was last updated.

Example: I run the code on the 01.10.2022 Im on vacation for 2 days The database was last updated on 01.10 On 04.10 im back and I run the code again. The code will look for the email from 02.10 & from 03.10 and of course also for the latest mail 04.10 extract the csv and extend the database

My first idea is to create a new folder where all the mails with subject “XYZ” automatically are moved. [Done.] Now I would check for the latest Date in my database. And now I have no clue how to proceed. I guess I need to loop though my new folder but only starting with mails which havent been extracted to the databse.

Firstly, there is no reason to loop though all messages in a folder - that would be extremely slow in folders with thousands of messages, especially if the cached mode is off.

Use Items.Restrict or Items.Find/FindNext - let the store provider do the heavy lifting for you. You will be able to specify a restriction on the Subject and/or ReceivedTime property". See the examples at https://learn.microsoft.com/en-us/office/vba/api/outlook.items.restrict

If you need to get items that correspond to your conditions (multiple) you need to use the Find / FindNext or Restrict methods of the Items class. Read more about these methods in the following articles that I wrote for the technical blog:

But when you need to search for items that correspond to your conditions in multiple folders and subfolders you can use use the AdvancedSearch method of the Application class. The key benefits of using the AdvancedSearch method in Outlook are:

  • The search is performed in another thread. You don't need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, ie beyond the scope of a certain folder. The Restrict and Find / FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
  • Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
  • You can stop the search process at any moment using the Stop method of the Search class.

Read more about the AdvancedSearch method in the Advanced search in Outlook programmatically: C#, VB.NET article.

Note, you may combine two search criteria using the logical AND operator and run the search once to get all items that correspond to your conditions. For example, the following search criteria string uses the ReceivedTime property to filter items:

  'This filter uses urn:schemas:httpmail namespace 
    strFilter = AddQuotes("urn:schemas:httpmail:datereceived") _ 
    & " > '" & datStartUTC & "' AND " _ 
    & AddQuotes("urn:schemas:httpmail:datereceived") _ 
    & " < '" & datEndUTC & "'" 

Outlook evaluates date-time values according to the time format, short date format, and long date format settings in the Regional and Language Options applet in the Windows Control Panel. In particular, Outlook evaluates time according to that specified time format without seconds. If you specify seconds in the date-time comparison string, the filter will not operate as expected.

Although dates and times are typically stored with a date format, filters using the Jet and DAV Searching and Locating (DASL) syntax require that the date-time value to be converted to a string representation. In Jet syntax, the date-time comparison string should be enclosed in either double quotes or single quotes. In DASL syntax, the date-time comparison string should be enclosed in single quotes.

To make sure that the date-time comparison string is formatted as Microsoft Outlook expects, use the Visual Basic for Applications Format function (or its equivalent in your programming language). Read more about that in the Filtering Items Using a Date-time Comparison article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM