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.

mail_email_code_login.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_email_code_login_mail_task(language: str, to: str, code: str) -> None:
  9. """
  10. Send email code login email with internationalization support.
  11. Args:
  12. language: Language code for email localization
  13. to: Recipient email address
  14. code: Email verification code
  15. """
  16. if not mail.is_inited():
  17. return
  18. logging.info(click.style(f"Start email code login 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.EMAIL_CODE_LOGIN,
  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 email code login mail to {to} succeeded: latency: {end_at - start_at}", fg="green")
  34. )
  35. except Exception:
  36. logging.exception("Send email code login mail to %s failed", to)