Wrap IMAP login attempt in a try except, log error if session timeout and exit script gracefully to prevent crashes, to address #568

This commit is contained in:
Garret Wassermann 2017-12-06 02:55:31 -05:00
parent a2c10edb35
commit 9340e149d5

View File

@ -13,6 +13,8 @@ scripts/get_email.py - Designed to be run from cron, this script checks the
from __future__ import unicode_literals
from datetime import timedelta
import base64
import binascii
import email
import imaplib
import mimetypes
@ -21,8 +23,8 @@ from os.path import isfile, join
import poplib
import re
import socket
import base64
import binascii
import ssl
import sys
from time import ctime
from bs4 import BeautifulSoup
@ -201,11 +203,20 @@ def process_queue(q, logger):
logger.info("Attempting IMAP server login")
try:
server.login(q.email_box_user or
settings.QUEUE_EMAIL_BOX_USER,
q.email_box_pass or
settings.QUEUE_EMAIL_BOX_PASSWORD)
server.select(q.email_box_imap_folder)
except imaplib.IMAP.abort:
logger.error("IMAP login failed. Check that the server is accessible and that the username and password are correct.")
server.logout()
sys.exit()
except ssl.SSLError:
logger.error("IMAP login failed due to SSL error. This is often due to a timeout. Please check your connection and try again.")
server.logout()
sys.exit()
try:
status, data = server.search(None, 'NOT', 'DELETED')