【发布时间】:2021-07-07 13:20:08
【问题描述】:
我正在使用 Python 和 O365,即 Microsoft Office 365 Python API 包。 这是包的链接:https://github.com/O365/python-o365
我正在编写一个脚本来自动发送具有某些属性的电子邮件,并跳过具有我不感兴趣的其他属性的电子邮件。
我过滤了所有电子邮件,只产生具有特定电子邮件主题的邮件,现在我正在进一步过滤它们。
这是我的代码:
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!")
continue 语句应该在找到 message_body 中的关键字 A 时跳过当前迭代。 注意:keywordA 在 blacklisted_keywordsA 列表中,KeywordB 在 blacklisted_keywordsB 列表中。
由于某种原因,它并没有跳过迭代,仍然将我不感兴趣的电子邮件写入文件,即使它包含列入黑名单的关键字,这种问题的可能解决方案是什么?
【问题讨论】: