diff --git a/config.py.example b/config.py.example index 1125981b0b6538304da4e4072632a8377934ef31..30d9180bff8d8894b151621273b2783cce837098 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 63c82259437b6e9e9117e8910d4bd278f4a88645..80ce03fa438078a932bdfdbfc98d53334c80a9df 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()