Start Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

Python Tool to connect to Outlook Inbox only pulling internal emails.

EC3
5 - Atom

Hello,

 

I am connecting to a shared inbox to gain insights into how the inbox is used. I am using the below code in the Python tool and am successfully connecting to the inbox, however it is only pulling in emails sent to the inbox from internal emails but most of the emails received in the inbox are from external emails. I am not familiar enough with Python to understand how to alter this code or if it is even possible.

-------------------------------------------------------------

import win32com.client
import pandas as pd

# Outlook Connection
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
#Folder Location
folder = outlook.Folders['Inbox Name'].Folders['Inbox']
#Message Details
messages = folder.Items

# Initialize Lists (prep dataframe columns)
senders = []
subject=[]
email = []
body =[]
categories =[]
senton = []
receivedtime = []

# Iterate through messages for sender name and address. Searches for only mail items (class 43 and email type EX), exchange user is populated (inactive employee's exchange name
#and email is removed). Appends sender, subject, and email.
for message in messages:
if message.Class==43:
if message.SenderEmailType=='EX':
if (message.Sender.GetExchangeUser())!= None:
senders.append (str(message.Sender))
subject.append(str(message.subject))
body.append(str(message.body))
categories.append(str(message.categories))
senton.append(str(message.senton))
receivedtime.append(str(message.receivedtime))
email.append(str(message.Sender.GetExchangeUser().PrimarySmtpAddress))

1 REPLY 1
apathetichell
20 - Arcturus

Here is what my buddy Claude said about your code:

 

Looking at your code, it's filtering for "internal messages" because of this specific condition:

if message.SenderEmailType=='EX':

The 'EX' email type indicates Exchange users - these are internal users within your organization's Exchange server. When Outlook stores messages from internal senders, it uses the Exchange address type ('EX') rather than SMTP addresses.
Here's what's happening:

Internal messages: Sender email type = 'EX' (Exchange)
External messages: Sender email type = 'SMTP' (internet email)

To include external messages as well, you have a few options:

 

 

 

Labels
Top Solution Authors