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_enterprise_task.py 1.1KB

1234567891011121314151617181920212223242526272829303132
  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_enterprise_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("Start enterprise mail to {} with subject {}".format(to, 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(
  21. click.style("Send enterprise mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green")
  22. )
  23. except Exception:
  24. logging.exception("Send enterprise mail to {} failed".format(to))