mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-13 01:19:02 +08:00
feat: add support for smtp when send email (#2409)
This commit is contained in:
parent
65a02f7d32
commit
71e5828d41
@ -81,11 +81,17 @@ UPLOAD_IMAGE_FILE_SIZE_LIMIT=10
|
|||||||
# Model Configuration
|
# Model Configuration
|
||||||
MULTIMODAL_SEND_IMAGE_FORMAT=base64
|
MULTIMODAL_SEND_IMAGE_FORMAT=base64
|
||||||
|
|
||||||
# Mail configuration, support: resend
|
# Mail configuration, support: resend, smtp
|
||||||
MAIL_TYPE=
|
MAIL_TYPE=resend
|
||||||
MAIL_DEFAULT_SEND_FROM=no-reply <no-reply@dify.ai>
|
MAIL_DEFAULT_SEND_FROM=no-reply <no-reply@dify.ai>
|
||||||
RESEND_API_KEY=
|
RESEND_API_KEY=
|
||||||
RESEND_API_URL=https://api.resend.com
|
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 configuration
|
||||||
SENTRY_DSN=
|
SENTRY_DSN=
|
||||||
|
@ -209,6 +209,12 @@ class Config:
|
|||||||
self.MAIL_DEFAULT_SEND_FROM = get_env('MAIL_DEFAULT_SEND_FROM')
|
self.MAIL_DEFAULT_SEND_FROM = get_env('MAIL_DEFAULT_SEND_FROM')
|
||||||
self.RESEND_API_KEY = get_env('RESEND_API_KEY')
|
self.RESEND_API_KEY = get_env('RESEND_API_KEY')
|
||||||
self.RESEND_API_URL = get_env('RESEND_API_URL')
|
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.
|
# Workpace Configurations.
|
||||||
|
@ -21,13 +21,27 @@ class Mail:
|
|||||||
api_key = app.config.get('RESEND_API_KEY')
|
api_key = app.config.get('RESEND_API_KEY')
|
||||||
if not api_key:
|
if not api_key:
|
||||||
raise ValueError('RESEND_API_KEY is not set')
|
raise ValueError('RESEND_API_KEY is not set')
|
||||||
|
|
||||||
api_url = app.config.get('RESEND_API_URL')
|
api_url = app.config.get('RESEND_API_URL')
|
||||||
if api_url:
|
if api_url:
|
||||||
resend.api_url = api_url
|
resend.api_url = api_url
|
||||||
|
|
||||||
resend.api_key = api_key
|
resend.api_key = api_key
|
||||||
self._client = resend.Emails
|
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:
|
else:
|
||||||
raise ValueError('Unsupported mail type {}'.format(app.config.get('MAIL_TYPE')))
|
raise ValueError('Unsupported mail type {}'.format(app.config.get('MAIL_TYPE')))
|
||||||
|
|
||||||
|
26
api/libs/smtp.py
Normal file
26
api/libs/smtp.py
Normal file
@ -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()
|
@ -104,10 +104,15 @@ services:
|
|||||||
MILVUS_PASSWORD: Milvus
|
MILVUS_PASSWORD: Milvus
|
||||||
# The milvus tls switch.
|
# The milvus tls switch.
|
||||||
MILVUS_SECURE: 'false'
|
MILVUS_SECURE: 'false'
|
||||||
# Mail configuration, support: resend
|
# Mail configuration, support: resend, smtp
|
||||||
MAIL_TYPE: ''
|
MAIL_TYPE: ''
|
||||||
# default send from email address, if not specified
|
# default send from email address, if not specified
|
||||||
MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply <no-reply@dify.ai>)'
|
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)
|
# the api-key for resend (https://resend.com)
|
||||||
RESEND_API_KEY: ''
|
RESEND_API_KEY: ''
|
||||||
RESEND_API_URL: https://api.resend.com
|
RESEND_API_URL: https://api.resend.com
|
||||||
|
Loading…
x
Reference in New Issue
Block a user