You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mail_email_code_login.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. from services.feature_service import FeatureService
  8. @shared_task(queue="mail")
  9. def send_email_code_login_mail_task(language: str, to: str, code: str):
  10. """
  11. Async Send email code login mail
  12. :param language: Language in which the email should be sent (e.g., 'en', 'zh')
  13. :param to: Recipient email address
  14. :param code: Email code to be included in the email
  15. """
  16. if not mail.is_inited():
  17. return
  18. logging.info(click.style("Start email code login mail to {}".format(to), fg="green"))
  19. start_at = time.perf_counter()
  20. # send email code login mail using different languages
  21. try:
  22. if language == "zh-Hans":
  23. template = "email_code_login_mail_template_zh-CN.html"
  24. system_features = FeatureService.get_system_features()
  25. if system_features.branding.enabled:
  26. application_title = system_features.branding.application_title
  27. template = "without-brand/email_code_login_mail_template_zh-CN.html"
  28. html_content = render_template(template, to=to, code=code, application_title=application_title)
  29. else:
  30. html_content = render_template(template, to=to, code=code)
  31. mail.send(to=to, subject="邮箱验证码", html=html_content)
  32. else:
  33. template = "email_code_login_mail_template_en-US.html"
  34. system_features = FeatureService.get_system_features()
  35. if system_features.branding.enabled:
  36. application_title = system_features.branding.application_title
  37. template = "without-brand/email_code_login_mail_template_en-US.html"
  38. html_content = render_template(template, to=to, code=code, application_title=application_title)
  39. else:
  40. html_content = render_template(template, to=to, code=code)
  41. mail.send(to=to, subject="Email Code", html=html_content)
  42. end_at = time.perf_counter()
  43. logging.info(
  44. click.style(
  45. "Send email code login mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green"
  46. )
  47. )
  48. except Exception:
  49. logging.exception("Send email code login mail to {} failed".format(to))