Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Fix closing connection to EmailBackend once done (fixes #218) #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions aldryn_forms/contrib/email_notifications/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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


Expand Down