Using the following code:
import imaplib
import time
user = '#my username'
password = '#my password'
server = 'imap.gmail.com'
mailbox = 'Inbox'
imap = imaplib.IMAP4_SSL(server)
imap.login(user, password)
while True:
try:
email_count = imap.select(mailbox, True)
results, data = imap.search(None, '(FROM "#TestEmail" SUBJECT "Test")')
print('starting search')
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
result, data = imap.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
print(raw_email)
print('email found, code will still run but no longer search')
exit()
except:
print("no email found, continuing search")
time.sleep(60)
exit()
The goal is to create a program that I can have running in the background when I am not at my office computer that will allow me to run certain processes when needed.
The issue that I am having is that the code:
Does not seem to refresh the list of emails in the inbox (i.e. if I send the email with the subject that should trigger a response, nothing ever happens (though if the email is already in the inbox when I start the code, the response does happen)).
I can not figure out a way to make the code exit after it has successfully completed the operation once. So basically I am trying to have it search for a particular key, and then once it has found that key and performed a task, it then stops searching for the key.
Alright, this is a dead thread, but I figured I would post the "working" script that I have figured out for this issue:
import imaplib
import time
import sys
user = '#username'
password = '#password'
server = 'imap.gmail.com'
mailbox = 'Inbox'
while True:
try:
imap = imaplib.IMAP4_SSL(server)
imap.login(user, password)
email_count = imap.select(mailbox, True)
results, data = imap.search(None, '(FROM "#username2" SUBJECT "Test2")')
print('starting search')
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
result, data = imap.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
print(raw_email)
imap.logout()
import Restart_Printer #this is a script that I have set up to restart my
#office printer
sys.exit()
except:
print("no email found, continuing search")
time.sleep(10)
So the reason that I say "working" is because this script works, but I'm not entirely sure why. The original script still won't actually stop running, but it will stop running the Restart_Printer.py script that the original script calls (I'm guessing this is because the Restart_Printer.py script does not have any end() or sys.exit commands in it, so it's possible that the original script is trying to launch an already active script).
But yeah this script basically searches your emails until it finds a particular key-word (in this case the subject line being "Test2") from a particular email address (I use a personal backup email address). The idea here is that when I am out of the office and someone sends me a message saying "hey the copy machine has stopped functioning (a common issue with the junk that we use)", I can just send that quick key-word to my email address and the script running on my cpu will detect it and perform the process for me.
Using this as a template you should be able to set up any other process that you would like your computer to run remotely, so feel free to play around with it!