Catch more email exceptiopns

This commit is contained in:
FinnStutzenstein 2019-04-02 16:08:52 +02:00
parent 29762ad89e
commit b058bda4f6
2 changed files with 24 additions and 2 deletions

View File

@ -254,7 +254,8 @@ class User(RESTModelMixin, PermissionsMixin, AbstractBaseUser):
except smtplib.SMTPDataError as e:
error = e.smtp_code
helptext = ""
if error == 554:
if error == 554: # The server does not accept our connection. The code is
# something like "transaction failed" or "No SMTP service here"
helptext = " Is the email sender correct?"
connection.close()
raise ValidationError(
@ -262,6 +263,13 @@ class User(RESTModelMixin, PermissionsMixin, AbstractBaseUser):
)
except smtplib.SMTPRecipientsRefused:
pass # Run into returning false later
except smtplib.SMTPAuthenticationError as e:
# Nice error message on auth failure
raise ValidationError(
{
"detail": f"Error {e.smtp_code}: Authentication failure. Please contact your local administrator."
}
)
else:
if count == 1:
self.email_send = True

View File

@ -665,7 +665,21 @@ class PasswordResetView(APIView):
subject = "".join(subject.splitlines())
from_email = None # TODO: Add nice from_email here.
email_message = mail.EmailMessage(subject, body, from_email, [to_email])
email_message.send()
try:
email_message.send()
except smtplib.SMTPRecipientsRefused:
raise ValidationError(
{
"detail": f"Error: The email to {to_email} was refused by the server. Please contact your local administrator."
}
)
except smtplib.SMTPAuthenticationError as e:
# Nice error message on auth failure
raise ValidationError(
{
"detail": f"Error {e.smtp_code}: Authentication failure. Please contact your administrator."
}
)
return super().post(request, *args, **kwargs)
def get_users(self, email):