From ba23234a15de2b7a993aa24c64506d0eba6e06c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 7 Aug 2019 23:25:52 +0200 Subject: [PATCH 1/3] Multiple attachments on mails --- src/utils/email.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/utils/email.py b/src/utils/email.py index 67821bf4..3307c03e 100644 --- a/src/utils/email.py +++ b/src/utils/email.py @@ -18,7 +18,7 @@ def _send_email( bcc_recipients=[], html_template="", sender="BornHack ", - attachment=None, + attachments=(), attachment_filename="", ): if not isinstance(to_recipients, list): @@ -42,10 +42,11 @@ def _send_email( msg.attach_alternative(html_template, "text/html") # is there an attachment to this mail? - if attachment: - # figure out the mimetype - mimetype = magic.from_buffer(attachment, mime=True) - msg.attach(attachment_filename, attachment, mimetype) + if attachments: + for attachment in attachments: + # figure out the mimetype + mimetype = magic.from_buffer(attachment, mime=True) + msg.attach(attachment_filename, attachment, mimetype) except Exception as e: logger.exception("exception while rendering email: {}".format(e)) return False From c088594e74abe9fd3e76ecbaf83000d324616ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 7 Aug 2019 23:29:13 +0200 Subject: [PATCH 2/3] Actually add more attachments --- src/utils/email.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/utils/email.py b/src/utils/email.py index 3307c03e..557ef6da 100644 --- a/src/utils/email.py +++ b/src/utils/email.py @@ -1,3 +1,5 @@ +from collections import namedtuple + from django.core.mail import EmailMultiAlternatives from django.core.validators import validate_email from django.core.exceptions import ValidationError @@ -9,6 +11,7 @@ import logging, magic logger = logging.getLogger("bornhack.%s" % __name__) +Attachment = namedtuple('Attachment', field_names=['filename', 'content']) def _send_email( text_template, @@ -18,8 +21,7 @@ def _send_email( bcc_recipients=[], html_template="", sender="BornHack ", - attachments=(), - attachment_filename="", + attachments=[], ): if not isinstance(to_recipients, list): to_recipients = [to_recipients] @@ -45,8 +47,8 @@ def _send_email( if attachments: for attachment in attachments: # figure out the mimetype - mimetype = magic.from_buffer(attachment, mime=True) - msg.attach(attachment_filename, attachment, mimetype) + mimetype = magic.from_buffer(attachment.content, mime=True) + msg.attach(attachment.filename, attachment.content, mimetype) except Exception as e: logger.exception("exception while rendering email: {}".format(e)) return False From 3600ed6190c799c535c011a69adac3b68fbe5ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 7 Aug 2019 23:33:56 +0200 Subject: [PATCH 3/3] Third times the charm? --- src/utils/email.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/utils/email.py b/src/utils/email.py index 557ef6da..6fdcd770 100644 --- a/src/utils/email.py +++ b/src/utils/email.py @@ -1,5 +1,3 @@ -from collections import namedtuple - from django.core.mail import EmailMultiAlternatives from django.core.validators import validate_email from django.core.exceptions import ValidationError @@ -11,7 +9,6 @@ import logging, magic logger = logging.getLogger("bornhack.%s" % __name__) -Attachment = namedtuple('Attachment', field_names=['filename', 'content']) def _send_email( text_template, @@ -21,7 +18,9 @@ def _send_email( bcc_recipients=[], html_template="", sender="BornHack ", - attachments=[], + attachment=None, + attachment_filename="", + attachments=None, ): if not isinstance(to_recipients, list): to_recipients = [to_recipients] @@ -43,12 +42,15 @@ def _send_email( if html_template: msg.attach_alternative(html_template, "text/html") + if not attachments and attachment: + attachments = [(attachment, attachment_filename)] + # is there an attachment to this mail? if attachments: - for attachment in attachments: + for content, filename in attachments: # figure out the mimetype - mimetype = magic.from_buffer(attachment.content, mime=True) - msg.attach(attachment.filename, attachment.content, mimetype) + mimetype = magic.from_buffer(content, mime=True) + msg.attach(filename, content, mimetype) except Exception as e: logger.exception("exception while rendering email: {}".format(e)) return False