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
|
2017-04-23 20:04:58 +00:00
|
|
|
from django.core.validators import validate_email
|
|
|
|
from .models import OutgoingEmail
|
2017-04-18 18:46:57 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
|
|
|
|
|
|
|
|
|
|
|
def _send_email(
|
|
|
|
text_template,
|
|
|
|
recipient,
|
|
|
|
subject,
|
2017-04-23 20:04:58 +00:00
|
|
|
html_template='',
|
2017-04-18 18:46:57 +00:00
|
|
|
sender='BornHack <info@bornhack.dk>',
|
2017-04-23 20:04:58 +00:00
|
|
|
attachment='',
|
|
|
|
attachment_filename=''
|
2017-04-18 18:46:57 +00:00
|
|
|
):
|
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,
|
2017-04-23 20:04:58 +00:00
|
|
|
text_template,
|
2017-04-18 18:46:57 +00:00
|
|
|
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(
|
2017-04-23 20:04:58 +00:00
|
|
|
html_template,
|
2017-04-18 18:46:57 +00:00
|
|
|
'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
|
2017-04-23 20:04:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_outgoing_email(
|
|
|
|
text_template,
|
|
|
|
recipients,
|
|
|
|
formatdict,
|
|
|
|
subject,
|
|
|
|
html_template='',
|
|
|
|
sender='BornHack <info@bornhack.dk>',
|
|
|
|
attachment='',
|
|
|
|
attachment_filename=''
|
|
|
|
):
|
|
|
|
""" adds an email to the outgoing queue
|
|
|
|
recipients is either just a str email or a str commaseperated emails
|
|
|
|
"""
|
|
|
|
text_template = render_to_string(text_template, formatdict)
|
|
|
|
|
|
|
|
if html_template:
|
|
|
|
html_template = render_to_string(html_template, formatdict)
|
|
|
|
|
|
|
|
if ',' in recipients:
|
|
|
|
for recipient in recipients.split(','):
|
|
|
|
validate_email(recipient.strip())
|
|
|
|
else:
|
|
|
|
validate_email(recipients)
|
|
|
|
|
|
|
|
OutgoingEmail.objects.create(
|
|
|
|
text_template=text_template,
|
|
|
|
html_template=html_template,
|
|
|
|
subject=subject,
|
|
|
|
sender=sender,
|
|
|
|
recipient=recipients,
|
|
|
|
attachment=attachment,
|
|
|
|
attachment_filename=attachment_filename
|
|
|
|
)
|
|
|
|
return True
|