From 9340e149d507b508c641583a0654c02b6c5d710d Mon Sep 17 00:00:00 2001 From: Garret Wassermann Date: Wed, 6 Dec 2017 02:55:31 -0500 Subject: [PATCH] Wrap IMAP login attempt in a try except, log error if session timeout and exit script gracefully to prevent crashes, to address #568 --- helpdesk/management/commands/get_email.py | 25 ++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index 6a52ec1b..d673a4ff 100755 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -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") - 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) + 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')