0

I have code that polls my outlook main inbox for a 2 factor authentication email, finds a string, and copies it to the web page and hits enter.

I would like to delete the email message after this.

I'm not sure where I've made the mistake in the code.

I didn't write the original python and intend to modify it to find the newest message and use that one instead of the len(messages)==1 criteria but I will still want to delete the message when I am done.

#Part 2: Retreive passcode from email for authentication
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6)
received_dt = datetime.now() - timedelta(minutes=5)
received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')
delay = 15 #seconds
waitloop = 1
passcodefound = 0
while passcodefound == 1:
    print ("Authentication email found.")
    break # it will break from the loop once the specific element will be present.
else:
    print("waiting for " + str(delay)+ " seconds.")
    time.sleep(delay)
    messages = inbox.Items
    messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")
    messages = messages.Restrict("[SenderEmailAddress] = '  '")
    messages = messages.Restrict("[Subject] = ' '")
    print("filtered inbox, " + str(len(messages))+" found.")
    if len(messages) == 1:
        for message in messages:
            text=message.Body
            CodeRegexVariable=re.compile(r'(\d\d\d\d\d\d)')
            code=CodeRegexVariable.search(str(text))
            answer=code.group()
            print(answer)
            print("2 Factor Authentication email found and processed.")
            passcodefound = 1
            passcode_field=driver.find_element(By.ID," ")
            passcode_field.clear()
            passcode_field.send_keys( )
            submit_button=driver.find_element(By.ID,"otpSubmitButton")
            submit_button.click()
            **message.Delete**
            break
    else:
        waitloop = waitloop+1
        total_wait_time = waitloop * delay
        print ("Authentication email not found. Wait time total = " + str(total_wait_time) + " seconds. Waiting for "+str(delay)+" seconds and trying again")

I moved the message.Delete line earlier to just after print("2 Factor Authentication email found and processed.") with the same result.

I unindented the message.Delete and break lines and i got an error for breaking outside of a loop I unindented the message.Delete and commented out the break with no error, but the message isn't deleted.

1

2 Answers 2

0

Firstly, you cannot restrict by calling Restrict multiple times - call Restrict once and combine all 3 criteria by concatenating them with the "AND" operator:

messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "' AND " & _
                             "[SenderEmailAddress] = '  ' AND " & _
                             "[Subject] = ' '")

Secondly, if you still want to delete all messages even if more than one found, chnage the if len(messages) == 1: condition to if len(messages) > 0:.

Sign up to request clarification or add additional context in comments.

2 Comments

This didn't work for me. Neither combining the filters or the deleting.
Please be more specific: what does "didn't work" mean? What is your actual query? Are you getting a particular error? What is it?
0

Try working with MailItem.Delete method (Outlook)

Example delete selected item

message.Delete()

import win32com.client    

def outlook_item(messages):
    if messages.Count > 0:
        print(messages.Count)
        for message in reversed(range(messages.Count, 0, -1)):
            print(message)
            message.Delete()
    else:
        print("Nothing selected.")



if __name__ == '__main__':
    olApp = win32com.client.Dispatch("outlook.Application")
    items = olApp.ActiveExplorer().Selection
    outlook_item(items)

Comments

Your Answer

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.