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 __future__ import unicode_literals
from datetime import timedelta from datetime import timedelta
import base64
import binascii
import email import email
import imaplib import imaplib
import mimetypes import mimetypes
@ -21,8 +23,8 @@ from os.path import isfile, join
import poplib import poplib
import re import re
import socket import socket
import base64 import ssl
import binascii import sys
from time import ctime from time import ctime
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -201,11 +203,20 @@ def process_queue(q, logger):
logger.info("Attempting IMAP server login") logger.info("Attempting IMAP server login")
server.login(q.email_box_user or try:
settings.QUEUE_EMAIL_BOX_USER, server.login(q.email_box_user or
q.email_box_pass or settings.QUEUE_EMAIL_BOX_USER,
settings.QUEUE_EMAIL_BOX_PASSWORD) q.email_box_pass or
server.select(q.email_box_imap_folder) 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: try:
status, data = server.search(None, 'NOT', 'DELETED') status, data = server.search(None, 'NOT', 'DELETED')