diff --git a/api/.env.example b/api/.env.example index 4f69c21a62..bcbc0c5867 100644 --- a/api/.env.example +++ b/api/.env.example @@ -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= diff --git a/api/config.py b/api/config.py index 7627e94e88..649a98b7bd 100644 --- a/api/config.py +++ b/api/config.py @@ -295,6 +295,7 @@ class Config: 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. diff --git a/api/extensions/ext_mail.py b/api/extensions/ext_mail.py index 32ac3fc717..ec3a5cc112 100644 --- a/api/extensions/ext_mail.py +++ b/api/extensions/ext_mail.py @@ -33,13 +33,16 @@ class Mail: 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'))) diff --git a/api/libs/smtp.py b/api/libs/smtp.py index f5735cbb2e..bf3a1a92e9 100644 --- a/api/libs/smtp.py +++ b/api/libs/smtp.py @@ -5,20 +5,27 @@ from email.mime.text import MIMEText 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) diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 84dd55ffaf..a04853d991 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -139,10 +139,11 @@ services: # default send from email address, if not specified MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply )' 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 @@ -268,10 +269,11 @@ services: # default send from email address, if not specified MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply )' 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