2017-04-18 18:46:57 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
from django.conf import settings
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
|
|
|
|
|
|
|
|
|
|
|
def _send_email(
|
|
|
|
text_template,
|
|
|
|
recipient,
|
|
|
|
formatdict,
|
|
|
|
subject,
|
|
|
|
html_template=None,
|
|
|
|
sender='BornHack <info@bornhack.dk>',
|
|
|
|
attachment=None,
|
|
|
|
attachment_filename=None
|
|
|
|
):
|
2017-04-21 22:23:12 +00:00
|
|
|
if not isinstance(recipient, list):
|
|
|
|
recipient = [recipient]
|
|
|
|
|
2017-04-18 18:46:57 +00:00
|
|
|
try:
|
|
|
|
# put the basic email together
|
|
|
|
msg = EmailMultiAlternatives(
|
|
|
|
subject,
|
|
|
|
render_to_string(text_template, formatdict),
|
|
|
|
sender,
|
2017-04-21 22:23:12 +00:00
|
|
|
recipient,
|
2017-04-18 18:46:57 +00:00
|
|
|
[settings.ARCHIVE_EMAIL]
|
|
|
|
)
|
|
|
|
|
|
|
|
# is there a html version of this email?
|
|
|
|
if html_template:
|
|
|
|
msg.attach_alternative(
|
|
|
|
render_to_string(html_template, formatdict),
|
|
|
|
'text/html'
|
|
|
|
)
|
|
|
|
|
|
|
|
# is there a pdf attachment to this mail?
|
|
|
|
if attachment:
|
|
|
|
msg.attach(attachment_filename, attachment, 'application/pdf')
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception('exception while rendering email: {}'.format(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
# send the email
|
|
|
|
msg.send()
|
|
|
|
|
|
|
|
return True
|