Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable working TLS to smtp.office365.com #166

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ services:

- SMTP=false ## true = enable mail notifications via smtp
- SMTP_HOST=smtp.gmail.com ## smtp Server - smtp.gmail.com for google
- SMTP_PORT=465 ## smtp Server Port - 465 for google
- SMTP_PORT=587 ## smtp Server Port - 587 for TLS, 465 for SSL, 25 for unsecured
- SMTP_USERNAME=max.mustermann@gmail.com ## smtp Login - your EMail for google
- SMTP_PASSWORD= ## smtp Login Password - your Login PW for google
- SMTP_TLS=true ## enable TLS - true for google and most smtp servers
- SMTP_TLS=true ## enable TLS
- SMTP_SSL=false ## enable SSL
- SMTP_SENDER=max.mustermann@gmail.com ## sender adress - same as Login for google and most smtp servers
- SMTP_RECIPIENT=max.mustermann@gmail.com ## recipient for notifications - can be the same as sender
#-SMTP_SUBJECT= ## Subject and Body options are optional.
Expand Down
3 changes: 2 additions & 1 deletion sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ TGTG_USER_ID=

SMTP=false
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_PORT=587
SMTP_USERNAME=
SMTP_PASSWORD=
SMTP_TLS=true
SMTP_SSL=false
SMTP_SENDER=
SMTP_RECIPIENT=

Expand Down
7 changes: 4 additions & 3 deletions src/config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ Username =
## Subject and Body options are optional.
## Subject and Body options can use variables as described below
## The Body option is interpreted as HTML
enabled = true
enabled = false
Host = smtp.gmail.com
Port = 465
Port = 587
Username = max.mustermann@gmail.com
Password =
TLS = true
TLS = true
SSL = false
Sender = max.mustermann@gmail.com
Recipient = max.mustermann@gmail.com
# cron =
Expand Down
2 changes: 2 additions & 0 deletions src/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _ini_reader(self, file: str) -> None:
"host": config["SMTP"].get("Host"),
"port": config["SMTP"].getint("Port"),
"tls": config["SMTP"].getboolean("TLS"),
"ssl": config["SMTP"].getboolean("SSL"),
"username": config["SMTP"].get("Username"),
"password": config["SMTP"].get("Password"),
"cron": config["SMTP"].get("cron", '* * * * *'),
Expand Down Expand Up @@ -167,6 +168,7 @@ def _env_reader(self) -> None:
"host": environ.get("SMTP_HOST", None),
"port": int(environ.get("SMTP_PORT", 25)),
"tls": environ.get("SMTP_TLS", "false").lower() in ('true', '1', 't'),
"ssl": environ.get("SMTP_SSL", "false").lower() in ('true', '1', 't'),
"username": environ.get("SMTP_USERNAME", ""),
"password": environ.get("SMTP_PASSWORD", ""),
"sender": environ.get("SMTP_SENDER", None),
Expand Down
7 changes: 5 additions & 2 deletions src/notifiers/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, config: Config):
self.host = config.smtp["host"]
self.port = config.smtp["port"]
self.tls = config.smtp["tls"]
self.ssl = config.smtp["ssl"]
self.username = config.smtp["username"]
self.password = config.smtp["password"]
self.sender = config.smtp["sender"]
Expand All @@ -39,7 +40,7 @@ def __init__(self, config: Config):
try:
self._connect()
except Exception as exc:
raise SMTPConfigurationError() from exc
raise SMTPConfigurationError(exc) from exc

def __del__(self):
"""Closes SMTP connection when shutdown"""
Expand All @@ -51,10 +52,12 @@ def __del__(self):

def _connect(self) -> None:
"""Connect to SMTP Server"""
if self.tls:
if self.ssl:
self.server = smtplib.SMTP_SSL(self.host, self.port)
else:
self.server = smtplib.SMTP(self.host, self.port)
if self.tls:
self.server.starttls()
self.server.set_debuglevel(self.debug)
self.server.ehlo()
if self.username and self.password:
Expand Down