You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mail_reset_password_task.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task # type: ignore
  5. from extensions.ext_mail import mail
  6. from libs.email_i18n import EmailType, get_email_i18n_service
  7. @shared_task(queue="mail")
  8. def send_reset_password_mail_task(language: str, to: str, code: str) -> None:
  9. """
  10. Send reset password email with internationalization support.
  11. Args:
  12. language: Language code for email localization
  13. to: Recipient email address
  14. code: Reset password code
  15. """
  16. if not mail.is_inited():
  17. return
  18. logging.info(click.style("Start password reset mail to {}".format(to), fg="green"))
  19. start_at = time.perf_counter()
  20. try:
  21. email_service = get_email_i18n_service()
  22. email_service.send_email(
  23. email_type=EmailType.RESET_PASSWORD,
  24. language_code=language,
  25. to=to,
  26. template_context={
  27. "to": to,
  28. "code": code,
  29. },
  30. )
  31. end_at = time.perf_counter()
  32. logging.info(
  33. click.style(
  34. "Send password reset mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green"
  35. )
  36. )
  37. except Exception:
  38. logging.exception("Send password reset mail to {} failed".format(to))