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_invite_member_task.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task # type: ignore
  5. from configs import dify_config
  6. from extensions.ext_mail import mail
  7. from libs.email_i18n import EmailType, get_email_i18n_service
  8. @shared_task(queue="mail")
  9. def send_invite_member_mail_task(language: str, to: str, token: str, inviter_name: str, workspace_name: str) -> None:
  10. """
  11. Send invite member email with internationalization support.
  12. Args:
  13. language: Language code for email localization
  14. to: Recipient email address
  15. token: Invitation token
  16. inviter_name: Name of the person sending the invitation
  17. workspace_name: Name of the workspace
  18. """
  19. if not mail.is_inited():
  20. return
  21. logging.info(
  22. click.style("Start send invite member mail to {} in workspace {}".format(to, workspace_name), fg="green")
  23. )
  24. start_at = time.perf_counter()
  25. try:
  26. url = f"{dify_config.CONSOLE_WEB_URL}/activate?token={token}"
  27. email_service = get_email_i18n_service()
  28. email_service.send_email(
  29. email_type=EmailType.INVITE_MEMBER,
  30. language_code=language,
  31. to=to,
  32. template_context={
  33. "to": to,
  34. "inviter_name": inviter_name,
  35. "workspace_name": workspace_name,
  36. "url": url,
  37. },
  38. )
  39. end_at = time.perf_counter()
  40. logging.info(
  41. click.style(
  42. "Send invite member mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green"
  43. )
  44. )
  45. except Exception:
  46. logging.exception("Send invite member mail to {} failed".format(to))