From 06cb73b5b4cc3dbb3cd8b0ba2e68b5c9198a07d9 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 30 Oct 2017 12:49:30 +0200 Subject: [PATCH] Add error handling to send_mail --- webapp/views.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/webapp/views.py b/webapp/views.py index bf55d84..125368c 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -23,16 +23,19 @@ from django.core.mail import send_mail def send_email(to, subject, body): - success = send_mail( - subject, - body, - settings.DEFAULT_EMAIL_FROM, - [to], - fail_silently=False, - ) + try: + success = send_mail( + subject, + body, + settings.DEFAULT_EMAIL_FROM, + [to], + fail_silently=False, + ) + if success == 0: + raise Exception('Failed to send email!') - if success == 0: - raise Exception('Failed to send email!') + except Exception as ex: + logging.exception('Failed to send email.') @require_http_methods(["GET"])