Add support for SMTP authentication for email notification

CC 23bbee8271
This commit is contained in:
Simon Sapin 2019-01-25 18:41:16 +01:00
parent 0f2c4e0685
commit e4f1ce96bb
2 changed files with 10 additions and 2 deletions

View File

@ -88,6 +88,8 @@ DEFAULT_CONFIG = {
"send_email": False, "send_email": False,
"smtp_server": 'localhost', "smtp_server": 'localhost',
"smtp_port": 25, "smtp_port": 25,
"smtp_username": None,
"smtp_password": None,
"smtp_from": "noreply@flatisfy.org", "smtp_from": "noreply@flatisfy.org",
"smtp_to": [], "smtp_to": [],
# The web site url, to be used in email notifications. (doesn't matter # The web site url, to be used in email notifications. (doesn't matter
@ -149,6 +151,8 @@ def validate_config(config, check_with_data):
assert isinstance(config["send_email"], bool) assert isinstance(config["send_email"], bool)
assert config["smtp_server"] is None or isinstance(config["smtp_server"], str) # noqa: E501 assert config["smtp_server"] is None or isinstance(config["smtp_server"], str) # noqa: E501
assert config["smtp_port"] is None or isinstance(config["smtp_port"], int) # noqa: E501 assert config["smtp_port"] is None or isinstance(config["smtp_port"], int) # noqa: E501
assert config["smtp_username"] is None or isinstance(config["smtp_username"], str) # noqa: E501
assert config["smtp_password"] is None or isinstance(config["smtp_password"], str) # noqa: E501
assert config["smtp_to"] is None or isinstance(config["smtp_to"], list) assert config["smtp_to"] is None or isinstance(config["smtp_to"], list)
assert isinstance(config["store_personal_data"], bool) assert isinstance(config["store_personal_data"], bool)

View File

@ -16,7 +16,7 @@ from email.utils import formatdate, make_msgid
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
def send_email(server, port, subject, _from, _to, txt, html): def send_email(server, port, subject, _from, _to, txt, html, username=None, password=None):
""" """
Send an email Send an email
@ -33,6 +33,8 @@ def send_email(server, port, subject, _from, _to, txt, html):
return return
server = smtplib.SMTP(server, port) server = smtplib.SMTP(server, port)
if username or password:
server.login(username or "", password or "")
msg = MIMEMultipart('alternative') msg = MIMEMultipart('alternative')
msg['Subject'] = subject msg['Subject'] = subject
@ -110,4 +112,6 @@ def send_notification(config, flats):
config["smtp_from"], config["smtp_from"],
config["smtp_to"], config["smtp_to"],
txt, txt,
html) html,
config.get("smtp_username"),
config.get("smtp_password"))