-1

I want to know if there's anything in Python that can constantly read my Outlook messages every five minutes. I want it to just check my email every 5 minutes (3:05PM, 3:10PM, 3:15PM, etc.) and as soon as an email comes in my inbox saying with a subject line like "Hello what's up" I want Python to automatically trigger a piece of .py code.

What I know so far

I already know how to access Outlook -

import win32com.client
win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(1)
...
...

Some possibilities

I am currently looking at a couple links such as persistently running Python in the background, listening for oncoming emails with Python, how to trigger a script upon email receipt as well as a Youtube video that talks about how to run a python script upon sending an email.

Hopefully I'll be able to figure out exactly how to run my Python script upon receipt of email, but in the meantime this question stays open. Thanks in advance.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Anders Zhou
  • 89
  • 1
  • 8
  • 1
    Please be aware this is not a code-writing or tutoring service. We can help solve specific, technical problems, not open-ended requests for code or advice. Please edit your question to show what you have tried so far, and what specific problem you need help with. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") page for details on how to best help us help you. – itprorh66 Jan 14 '22 at 18:19
  • @itprorh66 Hello, apologies, just edited my question - I'm doing some more research to figure this out – Anders Zhou Jan 15 '22 at 00:32
  • @itprorh66 I was finally able to answer my own question - posted below. – Anders Zhou Feb 08 '22 at 17:58

2 Answers2

2

thanks for your help. It's been a month, but I revisited this problem and was able to find a solution. While I couldn't find a python program that would listen for an Outlook email, VBA in Outlook could listen for an email and then kick off a python script.

My python code was originally geared to edit an Excel spreadsheet directly. So, when I received an email with a certain subject line blah for example, python file main.py would automatically execute and edit an excel file main.xlsx accordingly.

Something to note is that main.xlsx cannot be open at the time the email arrives. If anyone has any guidance on how to have main.xlsx be modified on the chance that the email arrives while it is open, I'd greatly appreciate it.

The below code solved my issue exactly:

Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim MessageInfo
Dim Result
If TypeName(Item) = "MailItem" Then
    Debug.Print "Arrived"
    If Item.Subject = "Blah" Then
        Const PyExe = """C:\...\...\...\...\...\python3.9\latest\python.exe"""
        Const PyScript = "R:\...\...\Anders\Python\main.py"
        
        Dim objShell As Object, cmd As String
        Set objShell = CreateObject("Wscript.Shell")
        
        cmd = PyExe & " " & PyScript
        Debug.Print cmd
        
        objShell.Run cmd
        objShell.exec cmd
        
        MsgBox objShell.exec(cmd).StdOut.ReadAll
    End If
End If
ExitNewItem:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ExitNewItem
End Sub
Anders Zhou
  • 89
  • 1
  • 8
0

The NewMailEx event of the Outlook Application class 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 NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.

In the NewMailEx event handler you may get an item instance received and check the Subject property.

You can either process the unread emails in the Inbox (see Items.Restrict or Items.Find/FindNext) assuming that new unprocessed messages are still unread or (in case of cached mode) use Items.ItemAdd event on the Inbox folder - it will fire when your OST file is being synchronized with the remote mailbox.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45