diff options
| author | zorun <github@bitsofnetworks.org> | 2020-05-21 21:13:33 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-21 21:13:33 +0200 |
| commit | df6ffc7d86b7334b2a3c309318cc8020cd8e781a (patch) | |
| tree | f4399df34a5d918f15ec17680878dff08459a19b /ihatemoney/utils.py | |
| parent | e10ea6c776339342dae098524742722c99d6b2bf (diff) | |
| download | ihatemoney-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/utils.py')
| -rw-r--r-- | ihatemoney/utils.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index 175b762..adced8f 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -7,6 +7,8 @@ from json import JSONEncoder, dumps import operator import os import re +import smtplib +import socket from babel import Locale from babel.numbers import get_currency_name, get_currency_symbol @@ -28,6 +30,22 @@ def slugify(value): return re.sub(r"[-\s]+", "-", value) +def send_email(mail_message): + """Send an email using Flask-Mail, with proper error handling. + + Return True if everything went well, and False if there was an error. + """ + # Since Python 3.4, SMTPException and socket.error are actually + # identical, but this was not the case before. Also, it is more clear + # to check for both. + try: + current_app.mail.send(mail_message) + except (smtplib.SMTPException, socket.error): + return False + # Email was sent successfully + return True + + class Redirect303(HTTPException, RoutingException): """Raise if the map requests a redirect. This is for example the case if |
