Django Library Users
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

emails.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from django.conf import settings
  2. from django.contrib.auth.models import User
  3. from django.contrib.sites.shortcuts import get_current_site
  4. from django.core.mail import EmailMessage, send_mail, mail_admins
  5. from django.template.loader import render_to_string
  6. from django.utils.encoding import force_bytes
  7. from django.utils.http import urlsafe_base64_encode
  8. from . tokens import generate_token
  9. from users import parameter
  10. def send_activation_mail(new_user, request):
  11. email_subject = "Activate Account"
  12. message2 = render_to_string('users/email_activation.html', {
  13. 'name': new_user.first_name,
  14. 'base_url': request.build_absolute_uri('/')[:-1],
  15. 'pk': new_user.pk,
  16. })
  17. email = EmailMessage(
  18. email_subject,
  19. message2,
  20. settings.EMAIL_HOST_USER,
  21. )
  22. mail_admins(email_subject, message2, fail_silently=True)
  23. def send_validation_failed(uid, token):
  24. subject = "E-Mail validation failed!"
  25. message = f"uid = {uid}\ntoken = '{token}'"
  26. mail_admins(subject, message, fail_silently=True)
  27. def send_recover_failed(uid, token):
  28. subject = "Password recovery failed!"
  29. message = f"uid = {uid}\ntoken = '{token}'"
  30. mail_admins(subject, message, fail_silently=True)
  31. def send_welcome_mail(new_user: User):
  32. subject = "Welcome!"
  33. message = """Hello %(username)s.
  34. Welcome!
  35. Thank you for registration.""" % {"username": new_user.first_name}
  36. message += "\n\n" + parameter.registration_flow_description(new_user.username)
  37. send_mail(subject, message, settings.EMAIL_FROM, [new_user.email], fail_silently=True)
  38. def send_validation_mail(new_user, request):
  39. email_subject = "Confirm your Email"
  40. message2 = render_to_string('users/email_confirmation.html', {
  41. 'name': new_user.first_name,
  42. 'base_url': request.build_absolute_uri('/')[:-1],
  43. 'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
  44. 'token': generate_token.make_token(new_user)
  45. })
  46. email = EmailMessage(
  47. email_subject,
  48. message2,
  49. settings.EMAIL_HOST_USER,
  50. [new_user.email],
  51. )
  52. send_mail(email_subject, message2, settings.EMAIL_FROM, [new_user.email], fail_silently=True)
  53. def send_recover_mail(user, request):
  54. email_subject = "Password recovery"
  55. message2 = render_to_string('users/email_recover.html', {
  56. 'name': user.first_name,
  57. 'base_url': request.build_absolute_uri('/')[:-1],
  58. 'uid': urlsafe_base64_encode(force_bytes(user.pk)),
  59. 'token': generate_token.make_token(user)
  60. })
  61. email = EmailMessage(
  62. email_subject,
  63. message2,
  64. settings.EMAIL_HOST_USER,
  65. [user.email],
  66. )
  67. send_mail(email_subject, message2, settings.EMAIL_FROM, [user.email], fail_silently=True)