Home > Software engineering >  How to trigger Python script when sending an Outlook email?
How to trigger Python script when sending an Outlook email?

Time:01-04

I would like to trigger a Python script when I send an email through Outlook.

So far I have only seen solutions to trigger a Python script when an email is received but not when sending an email.

CodePudding user response:

The ItemSend event of the Outlook Application class is fired for outgoing items, whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.

Be aware, Outlook should be opened and connected to your code (a valid Application instance retrieved). The event is not fired when Outlook is not running.

CodePudding user response:

With @EugeneAstafiev Answer, a python Example is

import pythoncom
from win32com.client import DispatchWithEvents


class SentMailEvents(object):    
    @staticmethod
    def OnItemSend(Item, event):
        print(f'The item being sent = {Item.Subject}')
        # do something with Item


if __name__ == "__main__":
    # Outlook events
    Outlook = DispatchWithEvents("outlook.Application", SentMailEvents)
    pythoncom.PumpMessages()
  • Related