mail_reset_password_task.py 1.5 KB

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