I made a program to run a function every time a new email is received. The IMAP server, connection and inbox work as intended. it is also not an issue with the whole '"Inbox"' vs 'Inbox' format. I have tried almost everything I could think of with little to no success.
My code:
def connect():
global connected
#sets up connection with imap server
connection = imaplib.IMAP4_SSL("outlook.office365.com")
try:
(retcode, capabilities) = connection.login(login, secure_password)
except:
print(sys.exc_info()[1])
sys.exit(1)
print("connection successful")
connected = True
return connection
def disconnect(connection):
connection.logout()
connected = False
connection = connect()
while connected == True:
print("Searching for new Emails...")
connection.select(mailbox = "Inbox", readonly=False)
# test section start
result, data_for_update = connection.uid('search', None, "ALL")
ids = data_for_update[0]
id_list = ids.split()
if data_for_update[0].split()[-1] == latest_email_uid:
print('No new emails found. search again in 5 minutes')
time.sleep(300)
else:
result, data_for_update = connection.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data_for_update[0],[1]
latest_email_uid == data_for_update[0].split()[-1]
print('New email found')
time.sleep(300)
I tried to program to run the code above. But when I run it, it returns this error:
\lib\imaplib.py", line 1055, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: UID command error: BAD [b'Command Argument Error. 11']