Django Library Users
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

emails.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_welcome_mail(new_user: User):
  28. subject = "Welcome!"
  29. message = """Hello %(username)s.
  30. Welcome!
  31. Thank you for registration.""" % {"username": new_user.first_name}
  32. message += "\n\n" + parameter.registration_flow_description(new_user.username)
  33. send_mail(subject, message, settings.EMAIL_FROM, [new_user.email], fail_silently=True)
  34. def send_validation_mail(new_user, request):
  35. email_subject = "Confirm your Email"
  36. message2 = render_to_string('users/email_confirmation.html', {
  37. 'name': new_user.first_name,
  38. 'base_url': request.build_absolute_uri('/')[:-1],
  39. 'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
  40. 'token': generate_token.make_token(new_user)
  41. })
  42. email = EmailMessage(
  43. email_subject,
  44. message2,
  45. settings.EMAIL_HOST_USER,
  46. [new_user.email],
  47. )
  48. send_mail(email_subject, message2, settings.EMAIL_FROM, [new_user.email], fail_silently=True)