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_account_deletion_task.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_deletion_success_task(to: str, language: str = "en-US") -> None:
  9. """
  10. Send account deletion success email with internationalization support.
  11. Args:
  12. to: Recipient email address
  13. language: Language code for email localization
  14. """
  15. if not mail.is_inited():
  16. return
  17. logging.info(click.style(f"Start send account deletion success email to {to}", fg="green"))
  18. start_at = time.perf_counter()
  19. try:
  20. email_service = get_email_i18n_service()
  21. email_service.send_email(
  22. email_type=EmailType.ACCOUNT_DELETION_SUCCESS,
  23. language_code=language,
  24. to=to,
  25. template_context={
  26. "to": to,
  27. "email": to,
  28. },
  29. )
  30. end_at = time.perf_counter()
  31. logging.info(
  32. click.style(
  33. "Send account deletion success email to {}: latency: {}".format(to, end_at - start_at), fg="green"
  34. )
  35. )
  36. except Exception:
  37. logging.exception("Send account deletion success email to {} failed".format(to))
  38. @shared_task(queue="mail")
  39. def send_account_deletion_verification_code(to: str, code: str, language: str = "en-US") -> None:
  40. """
  41. Send account deletion verification code email with internationalization support.
  42. Args:
  43. to: Recipient email address
  44. code: Verification code
  45. language: Language code for email localization
  46. """
  47. if not mail.is_inited():
  48. return
  49. logging.info(click.style(f"Start send account deletion verification code email to {to}", fg="green"))
  50. start_at = time.perf_counter()
  51. try:
  52. email_service = get_email_i18n_service()
  53. email_service.send_email(
  54. email_type=EmailType.ACCOUNT_DELETION_VERIFICATION,
  55. language_code=language,
  56. to=to,
  57. template_context={
  58. "to": to,
  59. "code": code,
  60. },
  61. )
  62. end_at = time.perf_counter()
  63. logging.info(
  64. click.style(
  65. "Send account deletion verification code email to {} succeeded: latency: {}".format(
  66. to, end_at - start_at
  67. ),
  68. fg="green",
  69. )
  70. )
  71. except Exception:
  72. logging.exception("Send account deletion verification code email to {} failed".format(to))