diff --git a/queued_mailer/tasks.py b/queued_mailer/tasks.py index 36ef200..a7cb348 100644 --- a/queued_mailer/tasks.py +++ b/queued_mailer/tasks.py @@ -1,5 +1,6 @@ # coding: utf-8 import smtplib +import ssl import time from socket import error as socket_error @@ -21,11 +22,12 @@ class PersitentEmailConnectionTask(celery.Task): def connection(self): if self._connection is None: self._connection = get_email_connection() - print('NEW CONN') - else: - print('OLD CONN') + return self._connection + def clean_connection(self): + self._connection = None + @celery.task(bind=True, base=PersitentEmailConnectionTask, queue=TASK_QUEUE_NAME, acks_late=True, default_retry_delay=20, autoretry_for=(EmailTransportException,), retry_kwargs={'max_retries': 5}) @@ -40,6 +42,10 @@ def send_message(self, email): try: email.connection = self.connection email.send() + except (ssl.SSLError, smtplib.SMTPServerDisconnected) as e: + # nullify connection + self.clean_connection() + raise self.retry(e, countdown=1) except (socket_error, smtplib.SMTPSenderRefused, smtplib.SMTPRecipientsRefused, smtplib.SMTPDataError,