Commit 584e03b070f8e6e6eb344ee18ee26fd183db138a

Authored by Zambom
1 parent 391777de

Sending email with configurated mail data

mailsender/templates/mailsender/update.html
... ... @@ -16,6 +16,12 @@
16 16 <form method="post" action="" enctype="multipart/form-data">
17 17 {% csrf_token %}
18 18 <legend>{% trans 'General server settings' %}</legend>
  19 + <em>
  20 + {% trans 'If your email host is Gmail make sure to turn on the option "Allow less secure apps" in' %}:
  21 + <b><a href="https://www.google.com/settings/security/lesssecureapps">https://www.google.com/settings/security/lesssecureapps</a></b>
  22 + </em>
  23 + <br clear="all" />
  24 + <br clear="all" />
19 25 {% for field in form %}
20 26 {% if field.auto_id == 'id_username' %}
21 27 <br clear="all" />
... ...
users/views.py
... ... @@ -22,6 +22,10 @@ from django.conf import settings
22 22 from django.utils.encoding import force_bytes
23 23 from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
24 24 from django.template import loader
  25 +from django.core.mail import EmailMessage
  26 +from django.core.mail.backends.smtp import EmailBackend
  27 +
  28 +from mailsender.models import MailSender
25 29  
26 30 #API IMPORTS
27 31 from rest_framework import viewsets
... ... @@ -346,15 +350,32 @@ class ForgotPassword(generic.FormView):
346 350 'protocol': 'http',
347 351 }
348 352  
349   - subject_template_name='registration/password_reset_subject.txt'
  353 + subject_template_name = 'registration/password_reset_subject.txt'
350 354 email_template_name = 'recover_pass_email_template.html'
351 355  
352 356 subject = loader.render_to_string(subject_template_name, c)
353 357 # Email subject *must not* contain newlines
354 358 subject = ''.join(subject.splitlines())
355 359 email = loader.render_to_string(email_template_name, c)
356   -
357   - send_mail(subject, email, settings.DEFAULT_FROM_EMAIL , [user.email], fail_silently=False)
  360 +
  361 + mailsender = MailSender.objects.get(id = 1)
  362 +
  363 + if mailsender.hostname == "example.com":
  364 + send_mail(subject, email, settings.DEFAULT_FROM_EMAIL , [user.email], fail_silently=False)
  365 + else:
  366 + if mailsender.crypto == 3 or mailsender.crypto == 4:
  367 + tls = True
  368 + else:
  369 + tls = False
  370 +
  371 + backend = EmailBackend(
  372 + host = mailsender.hostname, port = mailsender.port, username = mailsender.username,
  373 + password = mailsender.password, use_tls = tls, fail_silently = False
  374 + )
  375 +
  376 + mail_msg = EmailMessage(subject = subject, body = email, from_email = settings.DEFAULT_FROM_EMAIL, to = [user.email], connection = backend)
  377 +
  378 + mail_msg.send()
358 379  
359 380 result = self.form_valid(form)
360 381 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."))
... ...