0

I am looking to flag emails based on the sender on a shared outlook mailbox as quick as possible.

#imports:
import time
from time import strftime
import pandas as pd, win32com.client as client
from win32.com.client import Dispatch
#importing the excel file that contains email addresses and corresponding flags:
df_excel = pd.read_excel(r'\\user\...\addresses.xlsx')
#adding both columns as lists:
df_excel_mail = df_excel['mail'].tolist();df_excel_flag = df_excel['flag'].tolist()
outlook = client.Dispatch('Outlook.Application').GetNamespace('MAPI')
main_account = outlook.Folders.Item(1)
folder_inbox = main_account.Folders['Inbox'].Folders['Test']
folder_inbox_WIP = main_account.Folders['Inbox'].Folders['Test'].Folders['WIP']
while True:
 time.sleep(0)
 messages = folder_inbox.Items.Count
 if messages > 0:
 for i in reversed(range(0,messages)):
 message = folder_inbox.Item[i]
 for y, z, in zip(df_excel_mail,df_excel_flag)
 if message.Categories == '' and y == message.SenderEmailAddress
 message.Categories = z
 message.Save
 message.Move(folder_inbox_WIP)
 messages_v2 = folder_inbox_WIP.Items.Count
 if folder_inbox_WIP .Items.Count > 0:
 for ii in reversed (range(0,messages_v2)):
 message_v2 = folder_inbox_WIP[ii]
 message_v2.Move(folder_inbox)
 if strftime('%H, %M, %N') >= strftime('18:00:00')
 break

my addresses.xlsx sheet looks like that:

mail flag
[email protected] red color
[email protected] green color
... ...
[email protected]
... ...

When an email address is available in the df_excel_mail list but corresponding flag color is missing in the df_excel_flag list. some emails are sent to WIP folder anyway and flagged as #QNAN.

How is this possible when the following code is supposed to prevent that to happen ?

if message.Categories == '' and y == message.SenderEmailAddress
0m3r
12.5k15 gold badges40 silver badges77 bronze badges
asked Jun 16, 2021 at 10:22
10
  • first of it is certainly "respecting" whatever You have set there, have You checked what those variables are before evaluating them? Commented Jun 16, 2021 at 10:27
  • message.Categories is the flag/category attributed to an email in Outlook message.SenderEmailAddress is the email address stored in the excel sheet that I am looking to match with the email currently being scanned for potential flagging Commented Jun 16, 2021 at 10:31
  • check individually what they get evaluated to Commented Jun 16, 2021 at 10:39
  • not sure I am getting your right but I did run a bunch of test prior to this loop to test each feature and control it individually. message.Categories can be = '' if no flag assigned or to 'red color', 'green color', ... it works also with custom nammed categories. message.SenderEmailAddress is equal to address for current email being scanned by the loop y is equal to the mail column in the excel sheet Commented Jun 16, 2021 at 10:44
  • I guess those emails are strings but if python says they are not the same it means they are not the same, it could be some escaped character or sth like a newline that is making it not eval those both strings as both equal and to True Commented Jun 16, 2021 at 10:49

1 Answer 1

0

The problem was related to 'NaN' values in the dataframe I used .notna() to fix it

answered Jun 19, 2021 at 21:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.