diff --git a/aldryn_forms/contrib/email_notifications/cms_plugins.py b/aldryn_forms/contrib/email_notifications/cms_plugins.py index 713b85e2..f843d24b 100644 --- a/aldryn_forms/contrib/email_notifications/cms_plugins.py +++ b/aldryn_forms/contrib/email_notifications/cms_plugins.py @@ -168,15 +168,6 @@ def get_inline_instances(self, request, obj=None): return inlines def send_notifications(self, instance, form): - try: - connection = get_connection(fail_silently=False) - connection.open() - except: # noqa - # I use a "catch all" in order to not couple this handler to a specific email backend - # different email backends have different exceptions. - logger.exception("Could not send notification emails.") - return [] - notifications = instance.email_notifications.select_related('form') emails = [] @@ -192,11 +183,13 @@ def send_notifications(self, instance, form): recipients.append(parseaddr(to_email)) try: - connection.send_messages(emails) + with get_connection(fail_silently=False) as connection: + connection.send_messages(emails) except: # noqa - # again, we catch all exceptions to be backend agnostic + # we catch all exceptions to be backend agnostic logger.exception("Could not send notification emails.") recipients = [] + return recipients