From 584e03b070f8e6e6eb344ee18ee26fd183db138a Mon Sep 17 00:00:00 2001 From: Zambom Date: Thu, 12 Jan 2017 20:07:14 -0200 Subject: [PATCH] Sending email with configurated mail data --- mailsender/templates/mailsender/update.html | 6 ++++++ users/views.py | 27 ++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/mailsender/templates/mailsender/update.html b/mailsender/templates/mailsender/update.html index df78d0d..cff41ba 100644 --- a/mailsender/templates/mailsender/update.html +++ b/mailsender/templates/mailsender/update.html @@ -16,6 +16,12 @@
{% csrf_token %} {% trans 'General server settings' %} + + {% trans 'If your email host is Gmail make sure to turn on the option "Allow less secure apps" in' %}: + https://www.google.com/settings/security/lesssecureapps + +
+
{% for field in form %} {% if field.auto_id == 'id_username' %}
diff --git a/users/views.py b/users/views.py index f67c96c..422328b 100644 --- a/users/views.py +++ b/users/views.py @@ -22,6 +22,10 @@ from django.conf import settings from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode from django.template import loader +from django.core.mail import EmailMessage +from django.core.mail.backends.smtp import EmailBackend + +from mailsender.models import MailSender #API IMPORTS from rest_framework import viewsets @@ -346,15 +350,32 @@ class ForgotPassword(generic.FormView): 'protocol': 'http', } - subject_template_name='registration/password_reset_subject.txt' + subject_template_name = 'registration/password_reset_subject.txt' email_template_name = 'recover_pass_email_template.html' subject = loader.render_to_string(subject_template_name, c) # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) email = loader.render_to_string(email_template_name, c) - - send_mail(subject, email, settings.DEFAULT_FROM_EMAIL , [user.email], fail_silently=False) + + mailsender = MailSender.objects.get(id = 1) + + if mailsender.hostname == "example.com": + send_mail(subject, email, settings.DEFAULT_FROM_EMAIL , [user.email], fail_silently=False) + else: + if mailsender.crypto == 3 or mailsender.crypto == 4: + tls = True + else: + tls = False + + backend = EmailBackend( + host = mailsender.hostname, port = mailsender.port, username = mailsender.username, + password = mailsender.password, use_tls = tls, fail_silently = False + ) + + mail_msg = EmailMessage(subject = subject, body = email, from_email = settings.DEFAULT_FROM_EMAIL, to = [user.email], connection = backend) + + mail_msg.send() result = self.form_valid(form) messages.success(request, _("Soon you'll receive an email with instructions to set your new password. If you don't receive it in 24 hours, please check your spam box.")) -- libgit2 0.21.2