Wrap mail sending in try, except block in case of connection error

pull/6257/head
jacobtoppm 2020-03-30 17:40:19 +01:00 zatwierdzone przez Matt Westcott
rodzic 9946e39ea7
commit 6647825d13
1 zmienionych plików z 27 dodań i 25 usunięć

Wyświetl plik

@ -227,35 +227,37 @@ class EmailNotificationMixin:
def send_emails(self, template_set, context, recipients, **kwargs):
connection = get_connection()
sent_count = 0
try:
with OpenedConnection(connection) as open_connection:
# Send emails
for recipient in recipients:
try:
with OpenedConnection(connection) as open_connection:
# update context with this recipient
context["user"] = recipient
# Send emails
sent_count = 0
for recipient in recipients:
try:
# Translate text to the recipient language settings
with override(recipient.wagtail_userprofile.get_preferred_language()):
# Get email subject and content
email_subject = render_to_string(template_set['subject'], context).strip()
email_content = render_to_string(template_set['text'], context).strip()
# update context with this recipient
context["user"] = recipient
kwargs = {}
if getattr(settings, 'WAGTAILADMIN_NOTIFICATION_USE_HTML', False):
kwargs['html_message'] = render_to_string(template_set['html'], context)
# Translate text to the recipient language settings
with override(recipient.wagtail_userprofile.get_preferred_language()):
# Get email subject and content
email_subject = render_to_string(template_set['subject'], context).strip()
email_content = render_to_string(template_set['text'], context).strip()
kwargs = {}
if getattr(settings, 'WAGTAILADMIN_NOTIFICATION_USE_HTML', False):
kwargs['html_message'] = render_to_string(template_set['html'], context)
# Send email
send_mail(email_subject, email_content, [recipient.email], connection=open_connection, **kwargs)
sent_count += 1
except Exception:
logger.exception(
"Failed to send notification email '%s' to %s",
email_subject, recipient.email
)
# Send email
send_mail(email_subject, email_content, [recipient.email], connection=open_connection, **kwargs)
sent_count += 1
except Exception:
logger.exception(
"Failed to send notification email '%s' to %s",
email_subject, recipient.email
)
except (TimeoutError, ConnectionError):
logger.exception("Mail connection error, notification sending skipped")
return sent_count == len(recipients)