diff --git a/hc/lib/emails.py b/hc/lib/emails.py index fcbd0da2..04c44be1 100644 --- a/hc/lib/emails.py +++ b/hc/lib/emails.py @@ -1,4 +1,4 @@ -import smtplib +from smtplib import SMTPServerDisconnected, SMTPDataError from threading import Thread import time @@ -22,7 +22,7 @@ class EmailThread(Thread): self.message.send() # No exception--great! Return from the retry loop return - except smtplib.SMTPServerDisconnected as e: + except (SMTPServerDisconnected, SMTPDataError) as e: if attempt + 1 == self.MAX_TRIES: # This was the last attempt and it failed: # re-raise the exception diff --git a/hc/lib/tests/test_emails.py b/hc/lib/tests/test_emails.py index a0c5387d..cffc8ba1 100644 --- a/hc/lib/tests/test_emails.py +++ b/hc/lib/tests/test_emails.py @@ -1,4 +1,4 @@ -from smtplib import SMTPServerDisconnected +from smtplib import SMTPServerDisconnected, SMTPDataError from unittest.mock import Mock, patch from django.test import TestCase @@ -25,3 +25,12 @@ class EmailsTestCase(TestCase): t.run() self.assertEqual(mock_msg.send.call_count, 3) + + def test_it_retries_smtp_data_error(self, mock_time): + mock_msg = Mock() + mock_msg.send = Mock(side_effect=[SMTPDataError(454, "hello"), None]) + + t = EmailThread(mock_msg) + t.run() + + self.assertEqual(mock_msg.send.call_count, 2)