Reputation: 101
I have a piece of code which reads through Outlook emails and searches for emails based on specific criteria, however, the problem is that once all the emails have been found satisfying the criteria the code does not stop. It just keeps running even though it returns nothing more.
This is what I have so far:
I have tried using break statements and multiple if statements as well as elif. But I cannot seem to get it to work.
import win32com.client
from datetime import date, timedelta
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(18).Folders.Item("Riscunit")
messages = inbox.Items
date = date.today() - timedelta(days=1)
subject = "Catalyst"
for message in messages:
if subject in message.subject and date == message.senton.date():
print(message.sender)
print(message.senton.date())
print(message.senton.time())
print(message.body)
elif subject != message.subject and date != message.senton.date:
break
I would like the code to retrieve the relevant emails and then stop running. I am new to Python so any help would be much appreciated.
Upvotes: 0
Views: 156
Reputation: 39404
You may have to limit the number of messages you test to see whether they are what you want:
max = 42
for count, message in enumerate(messages):
if count > max:
break
if subject in message.subject and date == message.senton.date():
collect(message) # Do something with this message (print or append to list)
Upvotes: 1
Reputation: 719
elif
is unnecessary as it's the opposite of the if
if
is false, then an else
would be more appropriate--though it's likely you don' need an else
herebag = ['pizza', 'ziplocks','bananas', 'milk glass', 'post-its','spray']
foods = {1:'pizza', 2:'bananas', 3:'milk'}
# check shopping bag for food
for item in bag:
for key in foods:
if foods[key] in item:
print('eat ' + foods[key]);
break
else:
print('put ' + item + ' away');
print("done");
and here's another example that does something when your if condition is not met:
messages = [{'subject': 'shopping list', 'date': '05/10/2019', 'body': 'milk'},
{'subject': 'shopping list', 'date': '05/10/2019', 'body': 'pizza'},
{'subject': 'holiday', 'date': '12/10/2015', 'body': 'need vacation soon'},
{'subject': 'shopping list', 'date': '12/10/2015', 'body': 'we need potatoes'}]
date = '05/10/2019'
subject = "shopping list"
for item in messages:
for key in item:
if subject in item['subject'] and item['date'] == date:
print('buy ' + item['body']);
break
else:
print('archive \"' + item['body'] + '\" email');
break
print("done");
Upvotes: 1
Reputation: 7281
It is probably because of the logic of using elif
instead of else
.
Try this:
import win32com.client
from datetime import date, timedelta
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(18).Folders.Item("Riscunit")
messages = inbox.Items
date = date.today() - timedelta(days=1)
subject = "Catalyst"
for message in messages:
if subject in message.subject and date == message.senton.date():
print(message.sender)
print(message.senton.date())
print(message.senton.time())
print(message.body)
else:
subject != message.subject and date != message.senton.date
break
Upvotes: -1