From 5300cd91fd1a594405797e246cad9b0a850d32e5 Mon Sep 17 00:00:00 2001
From: nd <git@notandy.de>
Date: Sat, 19 Sep 2020 00:43:12 +0200
Subject: [PATCH] do not fail if mail could not be sent, closes #9

---
 uffd/selfservice/templates/set_password.html |  2 +-
 uffd/selfservice/views.py                    | 25 ++++++++++++--------
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/uffd/selfservice/templates/set_password.html b/uffd/selfservice/templates/set_password.html
index a60cfe0d..67f3a5a2 100644
--- a/uffd/selfservice/templates/set_password.html
+++ b/uffd/selfservice/templates/set_password.html
@@ -14,7 +14,7 @@
 			<label for="user-password1">New Password</label>
 			<input type="password" class="form-control" id="user-password1" name="password1" required="required" tabindex = "2">
 			<small class="form-text text-muted">
-				At least 8 characters, no other special requirements. But please don't be stupid and use a password manager.
+				At least 8 and at most 256 characters, no other special requirements. But please don't be stupid and use a password manager.
 			</small>
 		</div>
 		<div class="form-group col-12">
diff --git a/uffd/selfservice/views.py b/uffd/selfservice/views.py
index 1524fe98..c8c23758 100644
--- a/uffd/selfservice/views.py
+++ b/uffd/selfservice/views.py
@@ -145,13 +145,18 @@ def send_passwordreset(loginname):
 	send_mail(user.mail, msg)
 
 def send_mail(to_address, msg):
-	server = smtplib.SMTP(host=current_app.config['MAIL_SERVER'], port=current_app.config['MAIL_PORT'])
-	if current_app.config['MAIL_USE_STARTTLS']:
-		server.starttls()
-	server.login(current_app.config['MAIL_USERNAME'], current_app.config['MAIL_PASSWORD'])
-	msg['From'] = current_app.config['MAIL_FROM_ADDRESS']
-	msg['To'] = to_address
-	msg['Date'] = email.utils.formatdate(localtime=1)
-	msg['Message-ID'] = email.utils.make_msgid()
-	server.send_message(msg)
-	server.quit()
+	try:
+		server = smtplib.SMTP(host=current_app.config['MAIL_SERVER'], port=current_app.config['MAIL_PORT'])
+		if current_app.config['MAIL_USE_STARTTLS']:
+			server.starttls()
+		server.login(current_app.config['MAIL_USERNAME'], current_app.config['MAIL_PASSWORD'])
+		msg['From'] = current_app.config['MAIL_FROM_ADDRESS']
+		msg['To'] = to_address
+		msg['Date'] = email.utils.formatdate(localtime=1)
+		msg['Message-ID'] = email.utils.make_msgid()
+		server.send_message(msg)
+		server.quit()
+		return True
+	except SMTPException:
+		flash('Mail to "{}" could not be sent!'.format(to_address))
+		return False
-- 
GitLab