Explorar el Código

feat: add support for smtp when send email (#2409)

JonahCui hace 1 año
padre
commit
71e5828d41
Se han modificado 5 ficheros con 61 adiciones y 4 borrados
  1. 8 2
      api/.env.example
  2. 6 0
      api/config.py
  3. 15 1
      api/extensions/ext_mail.py
  4. 26 0
      api/libs/smtp.py
  5. 6 1
      docker/docker-compose.yaml

+ 8 - 2
api/.env.example

@@ -81,11 +81,17 @@ UPLOAD_IMAGE_FILE_SIZE_LIMIT=10
 # Model Configuration
 MULTIMODAL_SEND_IMAGE_FORMAT=base64
 
-# Mail configuration, support: resend
-MAIL_TYPE=
+# Mail configuration, support: resend, smtp
+MAIL_TYPE=resend
 MAIL_DEFAULT_SEND_FROM=no-reply <no-reply@dify.ai>
 RESEND_API_KEY=
 RESEND_API_URL=https://api.resend.com
+# smtp configuration
+SMTP_SERVER=smtp.gmail.com
+SMTP_PORT=587
+SMTP_USERNAME=123
+SMTP_PASSWORD=abc
+SMTP_USE_TLS=false
 
 # Sentry configuration
 SENTRY_DSN=

+ 6 - 0
api/config.py

@@ -209,6 +209,12 @@ class Config:
         self.MAIL_DEFAULT_SEND_FROM = get_env('MAIL_DEFAULT_SEND_FROM')
         self.RESEND_API_KEY = get_env('RESEND_API_KEY')
         self.RESEND_API_URL = get_env('RESEND_API_URL')
+        # SMTP settings
+        self.SMTP_SERVER = get_env('SMTP_SERVER')
+        self.SMTP_PORT = get_env('SMTP_PORT')
+        self.SMTP_USERNAME = get_env('SMTP_USERNAME')
+        self.SMTP_PASSWORD = get_env('SMTP_PASSWORD')
+        self.SMTP_USE_TLS = get_bool_env('SMTP_USE_TLS')
         
         # ------------------------
         # Workpace Configurations.

+ 15 - 1
api/extensions/ext_mail.py

@@ -21,13 +21,27 @@ class Mail:
                 api_key = app.config.get('RESEND_API_KEY')
                 if not api_key:
                     raise ValueError('RESEND_API_KEY is not set')
-                
+
                 api_url = app.config.get('RESEND_API_URL')
                 if api_url:
                     resend.api_url = api_url
 
                 resend.api_key = api_key
                 self._client = resend.Emails
+            elif app.config.get('MAIL_TYPE') == 'smtp':
+                from libs.smtp import SMTPClient
+                if not app.config.get('SMTP_SERVER') or not app.config.get('SMTP_PORT'):
+                    raise ValueError('SMTP_SERVER and SMTP_PORT are required for smtp mail type')
+                if not app.config.get('SMTP_USERNAME') or not app.config.get('SMTP_PASSWORD'):
+                    raise ValueError('SMTP_USERNAME and SMTP_PASSWORD are required for smtp mail type')
+                self._client = SMTPClient(
+                    server=app.config.get('SMTP_SERVER'),
+                    port=app.config.get('SMTP_PORT'),
+                    username=app.config.get('SMTP_USERNAME'),
+                    password=app.config.get('SMTP_PASSWORD'),
+                    _from=app.config.get('MAIL_DEFAULT_SEND_FROM'),
+                    use_tls=app.config.get('SMTP_USE_TLS')
+                )
             else:
                 raise ValueError('Unsupported mail type {}'.format(app.config.get('MAIL_TYPE')))
 

+ 26 - 0
api/libs/smtp.py

@@ -0,0 +1,26 @@
+import smtplib
+from email.mime.multipart import MIMEMultipart
+from email.mime.text import MIMEText
+
+
+class SMTPClient:
+    def __init__(self, server: str, port: int, username: str, password: str, _from: str, use_tls=False):
+        self.server = server
+        self.port = port
+        self._from = _from
+        self.username = username
+        self.password = password
+        self._use_tls = use_tls
+
+    def send(self, mail: dict):
+        smtp = smtplib.SMTP(self.server, self.port)
+        if self._use_tls:
+            smtp.starttls()
+        smtp.login(self.username, self.password)
+        msg = MIMEMultipart()
+        msg['Subject'] = mail['subject']
+        msg['From'] = self._from
+        msg['To'] = mail['to']
+        msg.attach(MIMEText(mail['html'], 'html'))
+        smtp.sendmail(self.username, mail['to'], msg.as_string())
+        smtp.quit()

+ 6 - 1
docker/docker-compose.yaml

@@ -104,10 +104,15 @@ services:
       MILVUS_PASSWORD: Milvus
       # The milvus tls switch.
       MILVUS_SECURE: 'false'
-      # Mail configuration, support: resend
+      # Mail configuration, support: resend, smtp
       MAIL_TYPE: ''
       # default send from email address, if not specified
       MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply <no-reply@dify.ai>)'
+      SMTP_HOST: ''
+      SMTP_PORT: 587
+      SMTP_USERNAME: ''
+      SMTP_PASSWORD: ''
+      SMTP_USE_TLS: 'true'
       # the api-key for resend (https://resend.com)
       RESEND_API_KEY: ''
       RESEND_API_URL: https://api.resend.com