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

Enable unauthenticated mail sending

parent de4fe2d3
No related branches found
No related tags found
No related merge requests found
...@@ -10,8 +10,9 @@ DEBUG = False ...@@ -10,8 +10,9 @@ DEBUG = False
MAIL_ACTIVE = True MAIL_ACTIVE = True
MAIL_FROM = "protokolle@example.com" MAIL_FROM = "protokolle@example.com"
MAIL_HOST = "mail.example.com:465" MAIL_HOST = "mail.example.com:465"
MAIL_USER = "user" MAIL_USER = "user" # set to "" for unauthenticated sending
MAIL_PASSWORD = "password" 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) # (local) message queue (necessary)
CELERY_BROKER_URL = "redis://localhost:6379/0" CELERY_BROKER_URL = "redis://localhost:6379/0"
......
...@@ -69,14 +69,12 @@ class MailManager: ...@@ -69,14 +69,12 @@ class MailManager:
self.hostname = getattr(config, "MAIL_HOST", "") self.hostname = getattr(config, "MAIL_HOST", "")
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)
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
or not self.hostname or not self.hostname
or not self.username
or not self.password
or not self.from_addr): or not self.from_addr):
print("Not sending mail {} to {}".format(subject, to_addr))
return return
msg = MIMEMultipart("mixed") # todo: test if clients accept attachment-free mails set to multipart/mixed msg = MIMEMultipart("mixed") # todo: test if clients accept attachment-free mails set to multipart/mixed
msg["From"] = self.from_addr msg["From"] = self.from_addr
...@@ -88,7 +86,8 @@ class MailManager: ...@@ -88,7 +86,8 @@ class MailManager:
part = MIMEApplication(file_like.read(), "octet-stream") part = MIMEApplication(file_like.read(), "octet-stream")
part["Content-Disposition"] = 'attachment; filename="{}"'.format(name) part["Content-Disposition"] = 'attachment; filename="{}"'.format(name)
msg.attach(part) msg.attach(part)
server = smtplib.SMTP_SSL(self.hostname) 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.login(self.username, self.password)
server.sendmail(self.from_addr, to_addr, msg.as_string()) server.sendmail(self.from_addr, to_addr, msg.as_string())
server.quit() server.quit()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment