mail_reset_password_task.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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',
  25. to=to,
  26. url=url)
  27. mail.send(to=to, subject="重置您的 Dify 密码", html=html_content)
  28. else:
  29. html_content = render_template('reset_password_mail_template_en-US.html',
  30. to=to,
  31. url=url)
  32. mail.send(to=to, subject="Reset Your Dify Password", html=html_content)
  33. end_at = time.perf_counter()
  34. logging.info(
  35. click.style('Send password reset mail to {} succeeded: latency: {}'.format(to, end_at - start_at),
  36. fg='green'))
  37. except Exception:
  38. logging.exception("Send password reset mail to {} failed".format(to))