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.

ext_mail.py 3.4KB

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