-1

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
ronmakh
  • 29
  • 6
  • What will the script do? Will it need to interact with Outlook further (eg extract information from the message that has been sent)? – DS_London Jan 01 '23 at 13:02

2 Answers2

1

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 1
    One nuance is that the `ItemSend` event passes a parameter called `Cancel` which allows the event handler to cancel the sending of the message if required. In VBA this is done by setting `Cancel=True` as the parameter is passed by reference. Python handles reference parameters differently so an explicit `return Cancel` is also needed at the end of the event handler function in order for the parameter to have any effect. `win32com` assigns the return variable(s) of the event handler function to the COM `[out]` parameter(s) in turn. – DS_London Jan 01 '23 at 14:03
  • Good to know. I am not a python guru. – Eugene Astafiev Jan 01 '23 at 18:13
  • Hi @EugeneAstafiev thanks for the information. How do I connect the code to Outlook? It seems that I can only run the code outside OutLook. Thanks so much for your advice – ronmakh Jan 03 '23 at 07:23
  • In Outlook you can create a VBA macro if required. Python can be used for automating Outlook only. – Eugene Astafiev Jan 03 '23 at 09:35
  • @ronmakh As I commented on the original post ... it depends what you want to do in the script and what information you need about the message being sent. As mentioned, you can write a VBA script which opens when Outlook opens, and reacts to the Send events. This VBA can then call (shell) a Python script. BUT it depends on how much information you need to pass to the Python script from the message (as this will be command line arguments to the script: you don't get simple access to the rich hierarchy of information in the message item). – DS_London Jan 03 '23 at 14:47
1

Try ItemSend event

import pythoncom
from win32com.client import DispatchWithEvents


# Event handler class for outlook events
class OutlookEvent(object):
    @staticmethod
    def OnItemSend(item, cancel):
        """
        Name    Required/Optional   Data type   Description
        Item    Required            Object      The item being sent.
        Cancel  Required            Boolean     
        
        cancel = False when the event occurs. 
        If the event procedure sets this argument to True, 
        the send action is not completed and the inspector is left open.
        """
        print(f'The item being sent = {item.Subject}')
            # # do something with Item


if __name__ == "__main__":
    Outlook = DispatchWithEvents("outlook.Application", OutlookEvent)
    pythoncom.PumpMessages()
    
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • https://stackoverflow.com/a/72710085/4539709 – 0m3r Jan 06 '23 at 17:36
  • As above, the `OnItemSend` event handler should have the line `return cancel` at the end if the `cancel` parameter is to have any effect when modified. – DS_London Jan 07 '23 at 11:08
  • Small point: the method doesn’t have to be static. The OutlookEvent class can have per-instance member variables (ie can have a `self` parameter). If you create the Application object with `Dispatch()` first you can then call WithEvents which returns an instance of the OutlookEvent handler class, which can have members set. This removes the need for global variables. – DS_London Jan 07 '23 at 11:20
  • Yes @DS_London, i agree, this is just quick working example – 0m3r Jan 08 '23 at 17:47