From 26a2d65c6f896c8b62aef1dc9a6f1641e6d06dac Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Sat, 15 Jul 2017 14:55:25 +0200
Subject: [PATCH] Add option to use STARTTLS for mails

/close #147
---
 config.py.example | 1 +
 utils.py          | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/config.py.example b/config.py.example
index 41fc0e6..a51dccb 100644
--- a/config.py.example
+++ b/config.py.example
@@ -13,6 +13,7 @@ MAIL_HOST = "mail.example.com:465"
 MAIL_USER = "user" # 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_STARTTLS = False # Usually, it's either this or SMTPS
 
 # (local) message queue (necessary)
 CELERY_BROKER_URL = "redis://localhost:6379/0"
diff --git a/utils.py b/utils.py
index dfc3fa1..0a31361 100644
--- a/utils.py
+++ b/utils.py
@@ -73,6 +73,7 @@ class MailManager:
         self.username = getattr(config, "MAIL_USER", "")
         self.password = getattr(config, "MAIL_PASSWORD", "")
         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):
         if (not self.active
@@ -91,6 +92,8 @@ class MailManager:
                 part["Content-Disposition"] = 'attachment; filename="{}"'.format(name)
                 msg.attach(part)
         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, ""]:
             server.login(self.username, self.password)
         server.sendmail(self.from_addr, to_addr.split(","), msg.as_string())
-- 
GitLab