| 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= |
| 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. |
| 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'))) |
| 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) | ||||
| # 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 |