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_change_mail_task.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_change_mail_task(language: str, to: str, code: str, phase: str):
  10. """
  11. Async Send change email 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: Change email code
  15. :param phase: Change email phase (new_email, old_email)
  16. """
  17. if not mail.is_inited():
  18. return
  19. logging.info(click.style("Start change email mail to {}".format(to), fg="green"))
  20. start_at = time.perf_counter()
  21. email_config = {
  22. "zh-Hans": {
  23. "old_email": {
  24. "subject": "检测您现在的邮箱",
  25. "template_with_brand": "change_mail_confirm_old_template_zh-CN.html",
  26. "template_without_brand": "without-brand/change_mail_confirm_old_template_zh-CN.html",
  27. },
  28. "new_email": {
  29. "subject": "确认您的邮箱地址变更",
  30. "template_with_brand": "change_mail_confirm_new_template_zh-CN.html",
  31. "template_without_brand": "without-brand/change_mail_confirm_new_template_zh-CN.html",
  32. },
  33. },
  34. "en": {
  35. "old_email": {
  36. "subject": "Check your current email",
  37. "template_with_brand": "change_mail_confirm_old_template_en-US.html",
  38. "template_without_brand": "without-brand/change_mail_confirm_old_template_en-US.html",
  39. },
  40. "new_email": {
  41. "subject": "Confirm your new email address",
  42. "template_with_brand": "change_mail_confirm_new_template_en-US.html",
  43. "template_without_brand": "without-brand/change_mail_confirm_new_template_en-US.html",
  44. },
  45. },
  46. }
  47. # send change email mail using different languages
  48. try:
  49. system_features = FeatureService.get_system_features()
  50. lang_key = "zh-Hans" if language == "zh-Hans" else "en"
  51. if phase not in ["old_email", "new_email"]:
  52. raise ValueError("Invalid phase")
  53. config = email_config[lang_key][phase]
  54. subject = config["subject"]
  55. if system_features.branding.enabled:
  56. template = config["template_without_brand"]
  57. else:
  58. template = config["template_with_brand"]
  59. html_content = render_template(template, to=to, code=code)
  60. mail.send(to=to, subject=subject, html=html_content)
  61. end_at = time.perf_counter()
  62. logging.info(
  63. click.style("Send change email mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green")
  64. )
  65. except Exception:
  66. logging.exception("Send change email mail to {} failed".format(to))