From 2147e03a541c41043a3f608b82feed0aced1b14e Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Wed, 1 Mar 2017 23:18:54 +0100
Subject: [PATCH] Enable unauthenticated mail sending

---
 config.py.example | 5 +++--
 utils.py          | 9 ++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/config.py.example b/config.py.example
index 1125981..30d9180 100644
--- a/config.py.example
+++ b/config.py.example
@@ -10,8 +10,9 @@ DEBUG = False
 MAIL_ACTIVE = True
 MAIL_FROM = "protokolle@example.com"
 MAIL_HOST = "mail.example.com:465"
-MAIL_USER = "user"
-MAIL_PASSWORD = "password"
+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)
 
 # (local) message queue (necessary)
 CELERY_BROKER_URL = "redis://localhost:6379/0"
diff --git a/utils.py b/utils.py
index 63c8225..80ce03f 100644
--- a/utils.py
+++ b/utils.py
@@ -69,14 +69,12 @@ class MailManager:
         self.hostname = getattr(config, "MAIL_HOST", "")
         self.username = getattr(config, "MAIL_USER", "")
         self.password = getattr(config, "MAIL_PASSWORD", "")
+        self.use_tls = getattr(config, "MAIL_USE_TLS", True)
 
     def send(self, to_addr, subject, content, appendix=None):
         if (not self.active
             or not self.hostname
-            or not self.username
-            or not self.password
             or not self.from_addr):
-            print("Not sending mail {} to {}".format(subject, to_addr))
             return
         msg = MIMEMultipart("mixed") # todo: test if clients accept attachment-free mails set to multipart/mixed
         msg["From"] = self.from_addr
@@ -88,8 +86,9 @@ class MailManager:
                 part = MIMEApplication(file_like.read(), "octet-stream")
                 part["Content-Disposition"] = 'attachment; filename="{}"'.format(name)
                 msg.attach(part)
-        server = smtplib.SMTP_SSL(self.hostname)
-        server.login(self.username, self.password)
+        server = (smtplib.SMTP_SSL if self.use_tls else smtplib.SMTP)(self.hostname)
+        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, msg.as_string())
         server.quit()
 
-- 
GitLab