Skip to content

Commit

Permalink
feat: add support for smtp when send email (#2409)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahcui authored Feb 7, 2024
1 parent 65a02f7 commit 71e5828
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
10 changes: 8 additions & 2 deletions api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion api/extensions/ext_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')))

Expand Down
26 changes: 26 additions & 0 deletions api/libs/smtp.py
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 6 additions & 1 deletion docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 71e5828

Please sign in to comment.