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.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task
  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(f"Start password reset mail to {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(f"Send password reset mail to {to} succeeded: latency: {end_at - start_at}", fg="green")
  34. )
  35. except Exception:
  36. logging.exception("Send password reset mail to %s failed", to)