Skip to content

Commit

Permalink
Enable working TLS to smtp.office365.com (#166)
Browse files Browse the repository at this point in the history
* Enable working TLS to smtp.office365.com

* added ssl and config options

* more details on SMTP errors, updated SMTP example to Gmail TLS

Co-authored-by: Henning Merklinger <[email protected]>
  • Loading branch information
Mike221002 and Der-Henning authored Oct 4, 2022
1 parent 05f3c6e commit b643c0a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
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
- [email protected] ## 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
- [email protected] ## sender adress - same as Login for google and most smtp servers
- [email protected] ## 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 = [email protected]
Password =
TLS = true
TLS = true
SSL = false
Sender = [email protected]
Recipient = [email protected]
# 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

0 comments on commit b643c0a

Please sign in to comment.