0

I have a table of clients, their emails, and a representative that they are assigned to:

Client Email Rep
1234 bob@bobsco.com John
5678 jim@jimsco.com Jane

The emails I receive from these clients go into an inbox that has subfolders with each rep's name.

I receive hundreds of these emails, and would like to sort them automatically.

How would I write a loop that would assign these emails to the correct rep's folder based on the email address that sent it? Alternatively, the email that gets sent out will always have the client number in the body, I could loop over that as well.

import win32com.client as client

# create outlook instance
outlook = client.Dispatch('Outlook.Application')

# get the namespace object
namespace = outlook.GetNameSpace("MAPI")

inbox = namespace.GetDefaultFolder(6)
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
benfo
  • 1
  • 1
  • Where is the table located? How do you find correspondence between emails and names (folder names)? – Eugene Astafiev Jan 03 '23 at 21:57
  • Use Outlook's filtering features. – Сергей Кох Jan 04 '23 at 04:19
  • @EugeneAstafiev right now it's just an Excel table that I'm reading into python – benfo Jan 04 '23 at 15:06
  • Hope you know how to read that data from an Excel, the rest is described in my post. – Eugene Astafiev Jan 04 '23 at 15:08
  • Why not let Outlook Rules do the work for you? Basically create a Rule and a Folder for each Rep. Use the "From" condition with a list of email addresses for that Rep (maybe an Address List, or semi-colon separated), and specify the "MoveTo" folder for the Rep. This could be set up with a script in either VBA or Python (and re-run when a new Rep or customer comes along). How many distinct customers and reps are we talking about? – DS_London Jan 04 '23 at 22:18
  • https://stackoverflow.com/a/29910853/4539709 – 0m3r Jan 05 '23 at 16:16

1 Answers1

0

To process incoming emails you need to handle the NewMailEx event of the Application class. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Use the Entry ID returned in the EntryIDCollection strign to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance.

Also you may consider handling the ItemAdd event of the Items class (on the folder) which is fired when items are added to the folder.

You can read more about possible solutions and their pros and cons in the series of articles:

Alternatively, the email that gets sent out will always have the client number in the body, I could loop over that as well.

In that case you need to handle the ItemSend event where you could set the MailItem.SaveSentMessageFolder property to put sent items to the folder specified. For example, the following VBA macro shows how to use the property in the code (the Outlook object model is common for all programming languages):

Sub SetSentFolder() 
 Dim myItem As Outlook.MailItem 
 Dim myResponse As Outlook.MailItem  
 Dim mpfInbox As Outlook.Folder  
 Dim mpf As Outlook.Folder 
 
 Set mpfInbox = Application.Session.GetDefaultFolder(olFolderInbox)  
 Set mpf = mpfInbox.Folders.Add("SaveToMySentItemsFolder")  
 Set myItem = Application.ActiveInspector.CurrentItem  
 Set myResponse = myItem.Reply  
 myResponse.Display  
 myResponse.To = "Eugene"  
 Set myResponse.SaveSentMessageFolder = mpf  
 myResponse.Send  
End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45