mail_reset_password_task.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task
  5. from flask import current_app, 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, token: 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 token: Reset password token to be included in the email
  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. url = f'{current_app.config.get("CONSOLE_WEB_URL")}/forgot-password?token={token}'
  22. if language == 'zh-Hans':
  23. html_content = render_template('reset_password_mail_template_zh-CN.html',
  24. to=to,
  25. url=url)
  26. mail.send(to=to, subject="重置您的 Dify 密码", html=html_content)
  27. else:
  28. html_content = render_template('reset_password_mail_template_en-US.html',
  29. to=to,
  30. url=url)
  31. mail.send(to=to, subject="Reset Your Dify Password", html=html_content)
  32. end_at = time.perf_counter()
  33. logging.info(
  34. click.style('Send password reset mail to {} succeeded: latency: {}'.format(to, end_at - start_at),
  35. fg='green'))
  36. except Exception:
  37. logging.exception("Send password reset mail to {} failed".format(to))