user4266493
user4266493

Reputation:

Retrieve new emails

I was wondering is there a way to get all unreaded email using Python?
I searched here but all I found was this, but I didn't understand answers.
Could we use imap4 for this?

All I could done was using

import imaplib

mailserver=imaplib.IMAP4.SSL('imap-mail.outlook.com:993')
mailserver.login('somthing','somthing')
status, count=mailserver.select('inbox')
status, data=mailserver.fetch(count[0], '(UID BODY[TEXT])')

print (data[0][1])

mailserver.close()
mailserver.logout()

and fetch the last email fron inbox like all of the tutorials shows

Upvotes: 0

Views: 100

Answers (1)

kenjoe41
kenjoe41

Reputation: 260

Since you are looking for only unread messages, you are going to have to search your inbox for the unread messages. More like this:

(status, data) = mail.search(None, '(UNSEEN)')
if status == 'OK':
   for num in data[0].split():
      #do something about your email

Upvotes: 1

Related Questions