Merge pull request #4563 from FinnStutzenstein/catchEmailExceptions

Catch more email exceptiopns
This commit is contained in:
Emanuel Schütze 2019-04-03 21:22:21 +02:00 committed by GitHub
commit ab0e83ff0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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])
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):