diff --git a/api/.env.example b/api/.env.example index 1e99a369acf65e..d492c1f8be26ee 100644 --- a/api/.env.example +++ b/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 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= diff --git a/api/config.py b/api/config.py index da23538b03e8f4..84572ff592c7dd 100644 --- a/api/config.py +++ b/api/config.py @@ -209,6 +209,12 @@ def __init__(self): 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. diff --git a/api/extensions/ext_mail.py b/api/extensions/ext_mail.py index c1e760431213f4..87b15697ba1995 100644 --- a/api/extensions/ext_mail.py +++ b/api/extensions/ext_mail.py @@ -21,13 +21,27 @@ def init_app(self, app: Flask): 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'))) diff --git a/api/libs/smtp.py b/api/libs/smtp.py new file mode 100644 index 00000000000000..17fcc6de7c2353 --- /dev/null +++ b/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() diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 81a85186acf08a..d8686b63bcc6a4 100644 --- a/docker/docker-compose.yaml +++ b/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 )' + 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