mjs / imapclient

An easy-to-use, Pythonic and complete IMAP client library
https://imapclient.readthedocs.io/
Other
516 stars 85 forks source link

Check new emails Continuously #366

Closed himanshusagar closed 5 years ago

himanshusagar commented 5 years ago

Hi, Is it possible to run a thread and check emails continuously and see if there's a new one ? A naive way would be to run following inside while loop:

  1. IMAPClient.login()
  2. Process unread emails
  3. Mark processed emails as read.
  4. IMAPClient.logout()
  5. sleep(5)
  6. Repeat

I am looking for something like login once and resynchronize with server for new unread emails? EDIT: I have tried IDLE mode using this and pulled UIDNEXT value of inbox folder. However, supplying same uid value(UIDNEXT) in server.fetch(), causes no results. I feel like i am missing some server.resynchronize_with_imap_server() command. Any thoughts?

MauPoLom commented 5 years ago

Did you find a solution to this problem. I'm looking for the same thing. It seems simple in NodJs https://github.com/jcreigno/nodejs-mail-notifier but there is no complete solution to be found on the net for python

himanshusagar commented 5 years ago

Hi MaPoLom,

Hope your're doing well. As it turns out, it is not possible to be notified for new emails in python. Your only option is to check for new/unread emails via some library( easyimap ) every 5/10 mins.

NicolasLM commented 5 years ago

It is definitely possible to do it with IMAPClient. A robust solution requires to use IMAP IDLE for notification and fallback to polling for mailboxes not supporting IDLE.

I used to listen for changes on thousands of mailboxes with a code similar to this one: https://github.com/mjs/imapclient/pull/324/files

mjs commented 5 years ago

Looking at nodejs-mail-notifier, it uses the 'mail' event from the imap package, which ultimately seems to be driven by IDLE notifications.