aboutsummaryrefslogtreecommitdiff
path: root/ihatemoney/web.py
diff options
context:
space:
mode:
authorzorun <github@bitsofnetworks.org>2020-05-21 21:13:33 +0200
committerGitHub <noreply@github.com>2020-05-21 21:13:33 +0200
commitdf6ffc7d86b7334b2a3c309318cc8020cd8e781a (patch)
treef4399df34a5d918f15ec17680878dff08459a19b /ihatemoney/web.py
parente10ea6c776339342dae098524742722c99d6b2bf (diff)
downloadihatemoney-mirror-df6ffc7d86b7334b2a3c309318cc8020cd8e781a.zip
ihatemoney-mirror-df6ffc7d86b7334b2a3c309318cc8020cd8e781a.tar.gz
ihatemoney-mirror-df6ffc7d86b7334b2a3c309318cc8020cd8e781a.tar.bz2
Improve error handling when sending emails (#595)
In one case, we were not catching a family of possible exceptions (socket.error), and in the two other cases there was no error handling at all. Sending emails can easily fail if no email server is configured, so it is really necessary to handle these errors instead of crashing with a HTTP 500 error. Refactor email sending code and add proper error handling. Show alert messages that tell the user if an email was sent or if there was an error. When sending a password reminder email or inviting people by email, we don't proceed to the next step in case of error, because sending emails is the whole point of these actions.
Diffstat (limited to 'ihatemoney/web.py')
-rw-r--r--ihatemoney/web.py66
1 files changed, 47 insertions, 19 deletions
diff --git a/ihatemoney/web.py b/ihatemoney/web.py
index ae124ac..6575720 100644
--- a/ihatemoney/web.py
+++ b/ihatemoney/web.py
@@ -12,7 +12,6 @@ from datetime import datetime
from functools import wraps
import json
import os
-from smtplib import SMTPRecipientsRefused
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
@@ -60,6 +59,7 @@ from ihatemoney.utils import (
list_of_dicts2json,
render_localized_template,
same_bill,
+ send_email,
)
main = Blueprint("main", __name__)
@@ -308,11 +308,21 @@ def create_project():
msg = Message(
message_title, body=message_body, recipients=[project.contact_email]
)
- try:
- current_app.mail.send(msg)
- except SMTPRecipientsRefused:
- flash(_("Error while sending reminder email"), category="danger")
-
+ success = send_email(msg)
+ if success:
+ flash(
+ _("A reminder email has just been sent to you"), category="success"
+ )
+ else:
+ # Display the error as a simple "info" alert, because it's
+ # not critical and doesn't prevent using the project.
+ flash(
+ _(
+ "We tried to send you an reminder email, but there was an error. "
+ "You can still use the project normally."
+ ),
+ category="info",
+ )
# redirect the user to the next step (invite)
flash(_("The project identifier is %(project)s", project=project.id))
return redirect(url_for(".list_bills", project_id=project.id))
@@ -328,17 +338,25 @@ def remind_password():
# get the project
project = Project.query.get(form.id.data)
# send a link to reset the password
- current_app.mail.send(
- Message(
- "password recovery",
- body=render_localized_template(
- "password_reminder", project=project
+ remind_message = Message(
+ "password recovery",
+ body=render_localized_template("password_reminder", project=project),
+ recipients=[project.contact_email],
+ )
+ success = send_email(remind_message)
+ if success:
+ return redirect(url_for(".password_reminder_sent"))
+ else:
+ flash(
+ _(
+ "Sorry, there was an error while sending you an email "
+ "with password reset instructions. "
+ "Please check the email configuration of the server "
+ "or contact the administrator."
),
- recipients=[project.contact_email],
+ category="danger",
)
- )
- return redirect(url_for(".password_reminder_sent"))
-
+ # Fall-through: we stay on the same page and display the form again
return render_template("password_reminder.html", form=form)
@@ -585,10 +603,20 @@ def invite():
body=message_body,
recipients=[email.strip() for email in form.emails.data.split(",")],
)
- current_app.mail.send(msg)
- flash(_("Your invitations have been sent"))
- return redirect(url_for(".list_bills"))
-
+ success = send_email(msg)
+ if success:
+ flash(_("Your invitations have been sent"), category="success")
+ return redirect(url_for(".list_bills"))
+ else:
+ flash(
+ _(
+ "Sorry, there was an error while trying to send the invitation emails. "
+ "Please check the email configuration of the server "
+ "or contact the administrator."
+ ),
+ category="danger",
+ )
+ # Fall-through: we stay on the same page and display the form again
return render_template("send_invites.html", form=form)