Python: IMAP connect to gmail returns errors

Go To StackoverFlow.com

3

Suddenly, my code that connects to gmail and checks for unread e-mails stopped working:

Connecting to Inbox..
Error
Traceback (most recent call last):
  File "./run", line 27, in <module>
    mail.login("xxx@dddd.com", "xxxxx123")
  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 890, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: LOGIN => socket error: EOF

Here's the code:

#!/usr/bin/env python

import imaplib, re
import os
import time
import socket

imap_host = 'imap.gmail.com'
mail = imaplib.IMAP4_SSL(imap_host, 993)
mail.login("xxxx@xxxx.com", "xxxx")

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 xxx-xx-203')
        time.sleep(30)
    except:
        print 'Error'
        time.sleep(120)
        imap_host = 'imap.gmail.com'
        mail = imaplib.IMAP4_SSL(imap_host, 993)
        mail.login("xxx@xxx.com", "xxxx")
        pass

And I also get this error a lot:

Traceback (most recent call last):
  File "./run", line 10, in <module>
    mail.login("xxx@xxx.com", "xxx")
  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
2012-04-04 23:33
by donald


0

Weird. I ran you exact code and connected to gmail just fine. I'd suggest trying to connect from another system.

If you don't have access to any other systems I could give you an account for you try on mine if you'd like.

2012-04-05 00:38
by sente
the problem seems to be with my account.. any idea why that's happening? When I try another, it seems to work - donald 2012-04-05 00:49
I belive gmail's webinterface allows you to enable/disable IMAP for your account. Have you verified it's enabled - WhyNotHugo 2012-04-05 01:08


0

This is bound to happen eventually. The solution is to catch the abort exception and to re-initialize your imap connection.

2012-04-14 05:58
by rjurney
I encountered the same problem, I will try your suggestion. But I do want to know the real cause of this - skyfree 2014-01-11 08:39
Ads