mail_account_deletion_task.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task # type: ignore
  5. from flask import render_template
  6. from extensions.ext_mail import mail
  7. @shared_task(queue="mail")
  8. def send_deletion_success_task(to):
  9. """Send email to user regarding account deletion.
  10. Args:
  11. log (AccountDeletionLog): Account deletion log object
  12. """
  13. if not mail.is_inited():
  14. return
  15. logging.info(click.style(f"Start send account deletion success email to {to}", fg="green"))
  16. start_at = time.perf_counter()
  17. try:
  18. html_content = render_template(
  19. "delete_account_success_template_en-US.html",
  20. to=to,
  21. email=to,
  22. )
  23. mail.send(to=to, subject="Your Dify.AI Account Has Been Successfully Deleted", html=html_content)
  24. end_at = time.perf_counter()
  25. logging.info(
  26. click.style(
  27. "Send account deletion success email to {}: latency: {}".format(to, end_at - start_at), fg="green"
  28. )
  29. )
  30. except Exception:
  31. logging.exception("Send account deletion success email to {} failed".format(to))
  32. @shared_task(queue="mail")
  33. def send_account_deletion_verification_code(to, code):
  34. """Send email to user regarding account deletion verification code.
  35. Args:
  36. to (str): Recipient email address
  37. code (str): Verification code
  38. """
  39. if not mail.is_inited():
  40. return
  41. logging.info(click.style(f"Start send account deletion verification code email to {to}", fg="green"))
  42. start_at = time.perf_counter()
  43. try:
  44. html_content = render_template("delete_account_code_email_template_en-US.html", to=to, code=code)
  45. mail.send(to=to, subject="Dify.AI Account Deletion and Verification", html=html_content)
  46. end_at = time.perf_counter()
  47. logging.info(
  48. click.style(
  49. "Send account deletion verification code email to {} succeeded: latency: {}".format(
  50. to, end_at - start_at
  51. ),
  52. fg="green",
  53. )
  54. )
  55. except Exception:
  56. logging.exception("Send account deletion verification code email to {} failed".format(to))