diff --git a/app/email_utils.py b/app/email_utils.py index b03ba831..3c672fc8 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -13,7 +13,7 @@ from email.message import Message from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import make_msgid, formatdate -from smtplib import SMTP, SMTPServerDisconnected +from smtplib import SMTP, SMTPServerDisconnected, SMTPException from typing import Tuple, List, Optional, Union import arrow @@ -315,6 +315,7 @@ def send_email_with_rate_control( html=None, max_nb_alert=MAX_ALERT_24H, nb_day=1, + ignore_smtp_error=False, ) -> bool: """Same as send_email with rate control over alert_type. Make sure no more than `max_nb_alert` emails are sent over the period of `nb_day` days @@ -341,7 +342,15 @@ def send_email_with_rate_control( SentAlert.create(user_id=user.id, alert_type=alert_type, to_email=to_email) db.session.commit() - send_email(to_email, subject, plaintext, html) + + if ignore_smtp_error: + try: + send_email(to_email, subject, plaintext, html) + except SMTPException: + LOG.w("Cannot send email to %s, subject %s", to_email, subject) + else: + send_email(to_email, subject, plaintext, html) + return True diff --git a/email_handler.py b/email_handler.py index e1484620..38f13384 100644 --- a/email_handler.py +++ b/email_handler.py @@ -1271,6 +1271,8 @@ def handle_bounce_forward_phase(msg: Message, email_log: EmailLog): mailbox_email=mailbox.email, ), max_nb_alert=10, + # smtp error can happen if user mailbox is unreachable, that might explain the bounce + ignore_smtp_error=True, ) else: LOG.w( @@ -1298,6 +1300,7 @@ def handle_bounce_forward_phase(msg: Message, email_log: EmailLog): mailbox_email=mailbox.email, ), max_nb_alert=10, + ignore_smtp_error=True, )