Python: Gmail Unread Error Messages

import imaplib, re import os import time import socket imap_host = 'imap.gmail.com' mail = imaplib.IMAP4_SSL(imap_host) mail.login(" xxx@example.com ", "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(" xxx@example.com ", "xxx") pass 

Works fine, but sometimes it crashes:

 Restarting processes... done Connecting to Inbox.. Error Traceback (most recent call last): File "gmail_new9.py", line 24, in <module> mail.login(" xxx@ccc.com ", "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?

+4
source share
2 answers

The reason your script crashes is because calling mail.login () inside the "except" block throws an exception that never gets thrown.

The imaplib documentation says that when you get an imaplib.abort exception, you should just re-run your command.

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

IMAP4.abort exception IMAP4 server errors cause this exception to be raised. This is a subclass of IMAP4.error. Note that closing an instance and creating a new instance will usually allow this exception.

Besides

 >>> help('imaplib') 

says the same thing:

"interruption" means that the connection must be reset, and the command is repeated.

Here's 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(" xxx@example.com ", "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() 
+13
source

Where is the port number for imap? Isn't it required? I used the code below and it works. Check if it works for you -

 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 can also try Gmail.py . I tried using this simple script that abstracts simple imap calls.

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

Keep in mind!! Gmail IMAP has known issues with clients requiring "too often" authentication. Among other things, this may mean that your account requires a CAPTCHA transfer to continue synchronization. Visit here to try to unlock, then try again.

+1
source

Source: https://habr.com/ru/post/1400249/


All Articles