Skip to content
Snippets Groups Projects
Commit 26a2d65c authored by Robin Sonnabend's avatar Robin Sonnabend
Browse files

Add option to use STARTTLS for mails

/close #147
parent 8c26a9a2
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ MAIL_HOST = "mail.example.com:465" ...@@ -13,6 +13,7 @@ MAIL_HOST = "mail.example.com:465"
MAIL_USER = "user" # set to "" for unauthenticated sending MAIL_USER = "user" # set to "" for unauthenticated sending
MAIL_PASSWORD = "password" # set to "" for unauthenticated sending MAIL_PASSWORD = "password" # set to "" for unauthenticated sending
MAIL_USE_TLS = True # should match the port in MAIL_HOST (if present there) MAIL_USE_TLS = True # should match the port in MAIL_HOST (if present there)
MAIL_USE_STARTTLS = False # Usually, it's either this or SMTPS
# (local) message queue (necessary) # (local) message queue (necessary)
CELERY_BROKER_URL = "redis://localhost:6379/0" CELERY_BROKER_URL = "redis://localhost:6379/0"
......
...@@ -73,6 +73,7 @@ class MailManager: ...@@ -73,6 +73,7 @@ class MailManager:
self.username = getattr(config, "MAIL_USER", "") self.username = getattr(config, "MAIL_USER", "")
self.password = getattr(config, "MAIL_PASSWORD", "") self.password = getattr(config, "MAIL_PASSWORD", "")
self.use_tls = getattr(config, "MAIL_USE_TLS", True) self.use_tls = getattr(config, "MAIL_USE_TLS", True)
self.use_starttls = getattr(config, "MAIL_USE_STARTTLS", False)
def send(self, to_addr, subject, content, appendix=None): def send(self, to_addr, subject, content, appendix=None):
if (not self.active if (not self.active
...@@ -91,6 +92,8 @@ class MailManager: ...@@ -91,6 +92,8 @@ class MailManager:
part["Content-Disposition"] = 'attachment; filename="{}"'.format(name) part["Content-Disposition"] = 'attachment; filename="{}"'.format(name)
msg.attach(part) msg.attach(part)
server = (smtplib.SMTP_SSL if self.use_tls else smtplib.SMTP)(self.hostname) server = (smtplib.SMTP_SSL if self.use_tls else smtplib.SMTP)(self.hostname)
if self.use_starttls:
server.starttls()
if self.username not in [None, ""] and self.password not in [None, ""]: if self.username not in [None, ""] and self.password not in [None, ""]:
server.login(self.username, self.password) server.login(self.username, self.password)
server.sendmail(self.from_addr, to_addr.split(","), msg.as_string()) server.sendmail(self.from_addr, to_addr.split(","), msg.as_string())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment