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

123456789101112131415161718192021222324252627282930
  1. import logging
  2. import time
  3. from collections.abc import Mapping
  4. import click
  5. from celery import shared_task # type: ignore
  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. @shared_task(queue="mail")
  10. def send_inner_email_task(to: list[str], subject: str, body: str, substitutions: Mapping[str, str]):
  11. if not mail.is_inited():
  12. return
  13. logging.info(click.style(f"Start enterprise mail to {to} with subject {subject}", fg="green"))
  14. start_at = time.perf_counter()
  15. try:
  16. html_content = render_template_string(body, **substitutions)
  17. email_service = get_email_i18n_service()
  18. email_service.send_raw_email(to=to, subject=subject, html_content=html_content)
  19. end_at = time.perf_counter()
  20. logging.info(click.style(f"Send enterprise mail to {to} succeeded: latency: {end_at - start_at}", fg="green"))
  21. except Exception:
  22. logging.exception("Send enterprise mail to %s failed", to)