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_inner_task.py 1.0KB

1234567891011121314151617181920212223242526272829303132
  1. import logging
  2. import time
  3. from collections.abc import Mapping
  4. import click
  5. from celery import shared_task
  6. from flask import render_template_string
  7. from extensions.ext_mail import mail
  8. from libs.email_i18n import get_email_i18n_service
  9. logger = logging.getLogger(__name__)
  10. @shared_task(queue="mail")
  11. def send_inner_email_task(to: list[str], subject: str, body: str, substitutions: Mapping[str, str]):
  12. if not mail.is_inited():
  13. return
  14. logger.info(click.style(f"Start enterprise mail to {to} with subject {subject}", fg="green"))
  15. start_at = time.perf_counter()
  16. try:
  17. html_content = render_template_string(body, **substitutions)
  18. email_service = get_email_i18n_service()
  19. email_service.send_raw_email(to=to, subject=subject, html_content=html_content)
  20. end_at = time.perf_counter()
  21. logger.info(click.style(f"Send enterprise mail to {to} succeeded: latency: {end_at - start_at}", fg="green"))
  22. except Exception:
  23. logger.exception("Send enterprise mail to %s failed", to)