Skip to content

Commit

Permalink
feat: opportunistic tls flag for smtp (#4794)
Browse files Browse the repository at this point in the history
  • Loading branch information
chazzhou authored May 30, 2024
1 parent 4b91383 commit a325a29
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
5 changes: 3 additions & 2 deletions api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ RESEND_API_KEY=
RESEND_API_URL=https://api.resend.com
# smtp configuration
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_PORT=465
SMTP_USERNAME=123
SMTP_PASSWORD=abc
SMTP_USE_TLS=false
SMTP_USE_TLS=true
SMTP_OPPORTUNISTIC_TLS=false

# Sentry configuration
SENTRY_DSN=
Expand Down
1 change: 1 addition & 0 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def __init__(self):
self.SMTP_USERNAME = get_env('SMTP_USERNAME')
self.SMTP_PASSWORD = get_env('SMTP_PASSWORD')
self.SMTP_USE_TLS = get_bool_env('SMTP_USE_TLS')
self.SMTP_OPPORTUNISTIC_TLS = get_bool_env('SMTP_OPPORTUNISTIC_TLS')

# ------------------------
# Workspace Configurations.
Expand Down
5 changes: 4 additions & 1 deletion api/extensions/ext_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ def init_app(self, app: Flask):
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_USE_TLS') and app.config.get('SMTP_OPPORTUNISTIC_TLS'):
raise ValueError('SMTP_OPPORTUNISTIC_TLS is not supported without enabling SMTP_USE_TLS')
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')
use_tls=app.config.get('SMTP_USE_TLS'),
opportunistic_tls=app.config.get('SMTP_OPPORTUNISTIC_TLS')
)
else:
raise ValueError('Unsupported mail type {}'.format(app.config.get('MAIL_TYPE')))
Expand Down
17 changes: 12 additions & 5 deletions api/libs/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@


class SMTPClient:
def __init__(self, server: str, port: int, username: str, password: str, _from: str, use_tls=False):
def __init__(self, server: str, port: int, username: str, password: str, _from: str, use_tls=False, opportunistic_tls=False):
self.server = server
self.port = port
self._from = _from
self.username = username
self.password = password
self._use_tls = use_tls
self.use_tls = use_tls
self.opportunistic_tls = opportunistic_tls

def send(self, mail: dict):
smtp = None
try:
smtp = smtplib.SMTP(self.server, self.port, timeout=10)
if self._use_tls:
smtp.starttls()
if self.use_tls:
if self.opportunistic_tls:
smtp = smtplib.SMTP(self.server, self.port, timeout=10)
smtp.starttls()
else:
smtp = smtplib.SMTP_SSL(self.server, self.port, timeout=10)
else:
smtp = smtplib.SMTP(self.server, self.port, timeout=10)

if self.username and self.password:
smtp.login(self.username, self.password)

Expand Down
6 changes: 4 additions & 2 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ services:
# default send from email address, if not specified
MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply <[email protected]>)'
SMTP_SERVER: ''
SMTP_PORT: 587
SMTP_PORT: 465
SMTP_USERNAME: ''
SMTP_PASSWORD: ''
SMTP_USE_TLS: 'true'
SMTP_OPPORTUNISTIC_TLS: 'false'
# the api-key for resend (https://resend.com)
RESEND_API_KEY: ''
RESEND_API_URL: https://api.resend.com
Expand Down Expand Up @@ -268,10 +269,11 @@ services:
# default send from email address, if not specified
MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply <[email protected]>)'
SMTP_SERVER: ''
SMTP_PORT: 587
SMTP_PORT: 465
SMTP_USERNAME: ''
SMTP_PASSWORD: ''
SMTP_USE_TLS: 'true'
SMTP_OPPORTUNISTIC_TLS: 'false'
# the api-key for resend (https://resend.com)
RESEND_API_KEY: ''
RESEND_API_URL: https://api.resend.com
Expand Down

1 comment on commit a325a29

@BorisPolonsky
Copy link
Contributor

@BorisPolonsky BorisPolonsky commented on a325a29 Jun 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on an update of dify deployment solution on k8s and found port465 is substituted for 587 in smtp config in this commit. May I know the reason, as document suggests 587 for TLS.

Please sign in to comment.