Browse Source

feat: opportunistic tls flag for smtp (#4794)

tags/0.6.10
Charles Zhou 1 year ago
parent
commit
a325a294bd
No account linked to committer's email address
5 changed files with 24 additions and 10 deletions
  1. 3
    2
      api/.env.example
  2. 1
    0
      api/config.py
  3. 4
    1
      api/extensions/ext_mail.py
  4. 12
    5
      api/libs/smtp.py
  5. 4
    2
      docker/docker-compose.yaml

+ 3
- 2
api/.env.example View File

RESEND_API_URL=https://api.resend.com RESEND_API_URL=https://api.resend.com
# smtp configuration # smtp configuration
SMTP_SERVER=smtp.gmail.com SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_PORT=465
SMTP_USERNAME=123 SMTP_USERNAME=123
SMTP_PASSWORD=abc SMTP_PASSWORD=abc
SMTP_USE_TLS=false
SMTP_USE_TLS=true
SMTP_OPPORTUNISTIC_TLS=false


# Sentry configuration # Sentry configuration
SENTRY_DSN= SENTRY_DSN=

+ 1
- 0
api/config.py View File

self.SMTP_USERNAME = get_env('SMTP_USERNAME') self.SMTP_USERNAME = get_env('SMTP_USERNAME')
self.SMTP_PASSWORD = get_env('SMTP_PASSWORD') self.SMTP_PASSWORD = get_env('SMTP_PASSWORD')
self.SMTP_USE_TLS = get_bool_env('SMTP_USE_TLS') self.SMTP_USE_TLS = get_bool_env('SMTP_USE_TLS')
self.SMTP_OPPORTUNISTIC_TLS = get_bool_env('SMTP_OPPORTUNISTIC_TLS')


# ------------------------ # ------------------------
# Workspace Configurations. # Workspace Configurations.

+ 4
- 1
api/extensions/ext_mail.py View File

from libs.smtp import SMTPClient from libs.smtp import SMTPClient
if not app.config.get('SMTP_SERVER') or not app.config.get('SMTP_PORT'): 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') 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( self._client = SMTPClient(
server=app.config.get('SMTP_SERVER'), server=app.config.get('SMTP_SERVER'),
port=app.config.get('SMTP_PORT'), port=app.config.get('SMTP_PORT'),
username=app.config.get('SMTP_USERNAME'), username=app.config.get('SMTP_USERNAME'),
password=app.config.get('SMTP_PASSWORD'), password=app.config.get('SMTP_PASSWORD'),
_from=app.config.get('MAIL_DEFAULT_SEND_FROM'), _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: else:
raise ValueError('Unsupported mail type {}'.format(app.config.get('MAIL_TYPE'))) raise ValueError('Unsupported mail type {}'.format(app.config.get('MAIL_TYPE')))

+ 12
- 5
api/libs/smtp.py View File





class SMTPClient: 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.server = server
self.port = port self.port = port
self._from = _from self._from = _from
self.username = username self.username = username
self.password = password self.password = password
self._use_tls = use_tls
self.use_tls = use_tls
self.opportunistic_tls = opportunistic_tls


def send(self, mail: dict): def send(self, mail: dict):
smtp = None smtp = None
try: 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: if self.username and self.password:
smtp.login(self.username, self.password) smtp.login(self.username, self.password)



+ 4
- 2
docker/docker-compose.yaml View File

# 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_SERVER: '' SMTP_SERVER: ''
SMTP_PORT: 587
SMTP_PORT: 465
SMTP_USERNAME: '' SMTP_USERNAME: ''
SMTP_PASSWORD: '' SMTP_PASSWORD: ''
SMTP_USE_TLS: 'true' SMTP_USE_TLS: 'true'
SMTP_OPPORTUNISTIC_TLS: 'false'
# 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
# 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_SERVER: '' SMTP_SERVER: ''
SMTP_PORT: 587
SMTP_PORT: 465
SMTP_USERNAME: '' SMTP_USERNAME: ''
SMTP_PASSWORD: '' SMTP_PASSWORD: ''
SMTP_USE_TLS: 'true' SMTP_USE_TLS: 'true'
SMTP_OPPORTUNISTIC_TLS: 'false'
# 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…
Cancel
Save