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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import logging
  2. from typing import Optional
  3. from flask import Flask
  4. from configs import dify_config
  5. from dify_app import DifyApp
  6. logger = logging.getLogger(__name__)
  7. class Mail:
  8. def __init__(self):
  9. self._client = None
  10. self._default_send_from = None
  11. def is_inited(self) -> bool:
  12. return self._client is not None
  13. def init_app(self, app: Flask):
  14. mail_type = dify_config.MAIL_TYPE
  15. if not mail_type:
  16. logger.warning("MAIL_TYPE is not set")
  17. return
  18. if dify_config.MAIL_DEFAULT_SEND_FROM:
  19. self._default_send_from = dify_config.MAIL_DEFAULT_SEND_FROM
  20. match mail_type:
  21. case "resend":
  22. import resend
  23. api_key = dify_config.RESEND_API_KEY
  24. if not api_key:
  25. raise ValueError("RESEND_API_KEY is not set")
  26. api_url = dify_config.RESEND_API_URL
  27. if api_url:
  28. resend.api_url = api_url
  29. resend.api_key = api_key
  30. self._client = resend.Emails
  31. case "smtp":
  32. from libs.smtp import SMTPClient
  33. if not dify_config.SMTP_SERVER or not dify_config.SMTP_PORT:
  34. raise ValueError("SMTP_SERVER and SMTP_PORT are required for smtp mail type")
  35. if not dify_config.SMTP_USE_TLS and dify_config.SMTP_OPPORTUNISTIC_TLS:
  36. raise ValueError("SMTP_OPPORTUNISTIC_TLS is not supported without enabling SMTP_USE_TLS")
  37. self._client = SMTPClient(
  38. server=dify_config.SMTP_SERVER,
  39. port=dify_config.SMTP_PORT,
  40. username=dify_config.SMTP_USERNAME or "",
  41. password=dify_config.SMTP_PASSWORD or "",
  42. _from=dify_config.MAIL_DEFAULT_SEND_FROM or "",
  43. use_tls=dify_config.SMTP_USE_TLS,
  44. opportunistic_tls=dify_config.SMTP_OPPORTUNISTIC_TLS,
  45. )
  46. case "sendgrid":
  47. from libs.sendgrid import SendGridClient
  48. if not dify_config.SENDGRID_API_KEY:
  49. raise ValueError("SENDGRID_API_KEY is required for SendGrid mail type")
  50. self._client = SendGridClient(
  51. sendgrid_api_key=dify_config.SENDGRID_API_KEY, _from=dify_config.MAIL_DEFAULT_SEND_FROM or ""
  52. )
  53. case _:
  54. raise ValueError(f"Unsupported mail type {mail_type}")
  55. def send(self, to: str, subject: str, html: str, from_: Optional[str] = None):
  56. if not self._client:
  57. raise ValueError("Mail client is not initialized")
  58. if not from_ and self._default_send_from:
  59. from_ = self._default_send_from
  60. if not from_:
  61. raise ValueError("mail from is not set")
  62. if not to:
  63. raise ValueError("mail to is not set")
  64. if not subject:
  65. raise ValueError("mail subject is not set")
  66. if not html:
  67. raise ValueError("mail html is not set")
  68. self._client.send(
  69. {
  70. "from": from_,
  71. "to": to,
  72. "subject": subject,
  73. "html": html,
  74. }
  75. )
  76. def is_enabled() -> bool:
  77. return dify_config.MAIL_TYPE is not None and dify_config.MAIL_TYPE != ""
  78. def init_app(app: DifyApp):
  79. mail.init_app(app)
  80. mail = Mail()