I am using Python and O365 the Microsoft Office 365 Python API package. Here is the link to the package: https://github.com/O365/python-o365
I am writing a script to automate sending emails with certain properties and to skip emails with other properties that I am not interested in.
I filtered all emails to only yield the ones with a specific email subject , now I am filtering them further.
Here is my code:
from O365 import Account, FileSystemTokenBackend, message, connection, MSGraphProtocol
import datetime
import traceback
import logging
todays_date = datetime.datetime.now()
todays_day = todays_date.day
#Accessing mailbox
mailbox = account.mailbox("myemail@email.com")
inbox = mailbox.inbox_folder()
junk_folder = mailbox.junk_folder()
messages_retrieved_from_inbox = inbox.get_messages()
messages_retrieved_from_junkfolder = junk_folder.get_messages(limit= 30, download_attachments= True)
#Taking care of messages
blacklisted_keywordsA = ['abc', 'def', 'ghi']
blacklisted_keywordsB = ['sometext', 'someothertext']
with open("myFile.txt", "a+", encoding="UTF-8") as my_file:
for message in messages_retrieved_from_junkfolder:
if(message.subject == "Some Subject" and message.created.day == todays_day):
print("Found Email with that subject that I am looking for!")
message_body = message.get_body_text()
for keywordA in blacklisted_keywordsA:
if(keywordA in message_body):
print("BlackListed keywordA ! Skip this inquiry.")
continue
for keywordB in blacklisted_keywordsB:
if(keywordB in message_body):
print("Blacklisted keywordB! Skip this inquiry.")
continue
my_file.write("My Message:\n")
my_file.write(message_body)
else:
print("Not Interested in this Email!")
The continue statement is supposed to skip the current iteration when it finds keywordA which is in the message_body. Note: keywordA is in blacklisted_keywordsA list and same for KeywordB which is in blacklisted_keywordsB list.
For some reason, it's not skipping the iteration and still writing the email that I am not interested in to the file, even though it contains blacklisted keywords, What is a possible solution to such problem ?
The issue is that continue
will continue the innermost loop. In this code:
for keywordB in blacklisted_keywordsB:
if(keywordB in message_body):
print("Blacklisted keywordB! Skip this inquiry.")
continue
The continue
statement will continue the for keywordB in....
loop, not the outer loop. See: https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
One option you have is to set a flag that gets checked when you do the write. Something like this:
skip_message = False
for keywordB in blacklisted_keywordsB:
if(keywordB in message_body):
print("Blacklisted keywordB! Skip this inquiry.")
skip_message = True
break #Break out of the keyword loop
if not skip_message:
my_file.write("My Message:\n")
my_file.write(message_body)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.