derive mimetype from attachment content rather than hardcoding application/pdf as we've done until now (we have non-pdf attachments going to economic now)

This commit is contained in:
Thomas Steen Rasmussen 2018-08-30 17:32:24 +02:00
parent 435f95b70b
commit 8d32170422

View file

@ -5,7 +5,7 @@ from django.core.files.base import ContentFile
from django.conf import settings from django.conf import settings
from django.template.loader import render_to_string from django.template.loader import render_to_string
from .models import OutgoingEmail from .models import OutgoingEmail
import logging import logging, magic
logger = logging.getLogger("bornhack.%s" % __name__) logger = logging.getLogger("bornhack.%s" % __name__)
@ -41,9 +41,11 @@ def _send_email(
'text/html' 'text/html'
) )
# is there a pdf attachment to this mail? # is there an attachment to this mail?
if attachment: if attachment:
msg.attach(attachment_filename, attachment, 'application/pdf') # figure out the mimetype
mimetype = magic.from_buffer(attachment, mime=True)
msg.attach(attachment_filename, attachment, 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