mail_reset_password_task.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task
  5. from flask import render_template
  6. from extensions.ext_mail import mail
  7. @shared_task(queue="mail")
  8. def send_reset_password_mail_task(language: str, to: str, code: str):
  9. """
  10. Async Send reset password mail
  11. :param language: Language in which the email should be sent (e.g., 'en', 'zh')
  12. :param to: Recipient email address
  13. :param code: Reset password code
  14. """
  15. if not mail.is_inited():
  16. return
  17. logging.info(click.style("Start password reset mail to {}".format(to), fg="green"))
  18. start_at = time.perf_counter()
  19. # send reset password mail using different languages
  20. try:
  21. if language == "zh-Hans":
  22. html_content = render_template("reset_password_mail_template_zh-CN.html", to=to, code=code)
  23. mail.send(to=to, subject="设置您的 Dify 密码", html=html_content)
  24. else:
  25. html_content = render_template("reset_password_mail_template_en-US.html", to=to, code=code)
  26. mail.send(to=to, subject="Set Your Dify Password", html=html_content)
  27. end_at = time.perf_counter()
  28. logging.info(
  29. click.style(
  30. "Send password reset mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green"
  31. )
  32. )
  33. except Exception:
  34. logging.exception("Send password reset mail to {} failed".format(to))