Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Gmail Unread Mails Crashes

import imaplib, re
import os
import time
import socket

imap_host = 'imap.gmail.com'
mail = imaplib.IMAP4_SSL(imap_host)
mail.login("[email protected]", "sddd")

while True:
    try:
        print 'Connecting to Inbox..'
        mail.select("inbox") # connect to inbox.
        result, data = mail.uid('search', None, 'UNSEEN')
        uid_list = data[0].split()
        print len(uid_list), 'Unseen emails.'
        if len(uid_list) > 20:
         os.system('heroku restart --app xx-xx-203')
        time.sleep(30)
    except:
        print 'Error'
        imap_host = 'imap.gmail.com'
        mail = imaplib.IMAP4_SSL(imap_host)
        mail.login("[email protected]", "xxx")
        pass

Works perfectly but sometimes it crashes with:

Restarting processes... done
Connecting to Inbox..
Error
Traceback (most recent call last):
  File "gmail_new9.py", line 24, in <module>
    mail.login("[email protected]", "ddddd")
  File "/usr/lib/python2.6/imaplib.py", line 498, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib/python2.6/imaplib.py", line 1060, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.6/imaplib.py", line 893, in _command_complete
    self._check_bye()
  File "/usr/lib/python2.6/imaplib.py", line 808, in _check_bye
    raise self.abort(bye[-1])
imaplib.abort: [UNAVAILABLE] Temporary System Error

How can I fix this?

like image 486
donald Avatar asked Mar 07 '12 16:03

donald


2 Answers

The reason your script crashes is that call to mail.login() inside "except" block throws an exception that is never caught.

Documentation to imaplib states that when you get imaplib.abort exception, you should just retry you command.

http://docs.python.org/library/imaplib

exception IMAP4.abort IMAP4 server errors cause this exception to be raised. This is a sub-class of IMAP4.error. Note that closing the instance and instantiating a new one will usually allow recovery from this exception.

Also

>>> help('imaplib')

says the same:

"abort" exceptions imply the connection should be reset, and the command re-tried.

Here is how you can fix it:

import imaplib, re
import os
import time
import socket

def connect(retries=5, delay=3):
    while True:
        try:
            imap_host = 'imap.gmail.com'
            mail = imaplib.IMAP4_SSL(imap_host)
            mail.login("[email protected]", "sddd")
            return mail
        except imaplib.IMAP4_SSL.abort:
            if retries > 0:
                retries -= 1
                time.sleep(delay)
            else:
                raise

mail = connect()
while True:
    try:
        print 'Connecting to Inbox..'
        mail.select("inbox") # connect to inbox.
        result, data = mail.uid('search', None, 'UNSEEN')
        uid_list = data[0].split()
        print len(uid_list), 'Unseen emails.'
        if len(uid_list) > 20:
         os.system('heroku restart --app xx-xx-203')
        time.sleep(30)
    except:
        print 'Error'
        mail = connect()
like image 54
subdir Avatar answered Nov 06 '22 06:11

subdir


Where is the port number for imap? Isnt that required? I have been using the below code & it works. Check if it works for you too -

import imaplib

gmail = imaplib.IMAP4_SSL('imap.gmail.com',993)
gmail.login('username','password')
gmail.select("inbox")
result, data = gmail.uid('search', None, 'UNSEEN')

You could also try Gmail.py. I tried using this simple script which abstracts simple imap calls.

from gmail import *

gmail = GmailClient()
gmail.login('username','password')
unreadMail = gmail.get_inbox_conversations(is_unread=True)
print unreadMail

Be aware!! Gmail IMAP has known issues with clients requesting to authenticate "too often." Among other things, this may flag your account to require passing a CAPTCHA to continue syncing. Visit here to attempt the unlock and then try again.

like image 20
Srikar Appalaraju Avatar answered Nov 06 '22 06:11

Srikar Appalaraju