简体   繁体   中英

How to trigger Python script when sending an Outlook email?

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.

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.

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()

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