Third times the charm?

This commit is contained in:
Víðir Valberg Guðmundsson 2019-08-07 23:33:56 +02:00
parent c088594e74
commit 3600ed6190

View file

@ -1,5 +1,3 @@
from collections import namedtuple
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from django.core.validators import validate_email from django.core.validators import validate_email
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -11,7 +9,6 @@ import logging, magic
logger = logging.getLogger("bornhack.%s" % __name__) logger = logging.getLogger("bornhack.%s" % __name__)
Attachment = namedtuple('Attachment', field_names=['filename', 'content'])
def _send_email( def _send_email(
text_template, text_template,
@ -21,7 +18,9 @@ def _send_email(
bcc_recipients=[], bcc_recipients=[],
html_template="", html_template="",
sender="BornHack <info@bornhack.dk>", sender="BornHack <info@bornhack.dk>",
attachments=[], attachment=None,
attachment_filename="",
attachments=None,
): ):
if not isinstance(to_recipients, list): if not isinstance(to_recipients, list):
to_recipients = [to_recipients] to_recipients = [to_recipients]
@ -43,12 +42,15 @@ def _send_email(
if html_template: if html_template:
msg.attach_alternative(html_template, "text/html") msg.attach_alternative(html_template, "text/html")
if not attachments and attachment:
attachments = [(attachment, attachment_filename)]
# is there an attachment to this mail? # is there an attachment to this mail?
if attachments: if attachments:
for attachment in attachments: for content, filename in attachments:
# figure out the mimetype # figure out the mimetype
mimetype = magic.from_buffer(attachment.content, mime=True) mimetype = magic.from_buffer(content, mime=True)
msg.attach(attachment.filename, attachment.content, mimetype) msg.attach(filename, content, mimetype)
except Exception as e: except Exception as e:
logger.exception("exception while rendering email: {}".format(e)) logger.exception("exception while rendering email: {}".format(e))
return False return False