2017-04-18 18:46:57 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2017-04-30 09:32:49 +00:00
|
|
|
from django.core.validators import validate_email
|
2017-05-21 13:04:51 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-04-30 09:32:49 +00:00
|
|
|
from django.core.files.base import ContentFile
|
2017-04-18 18:46:57 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.template.loader import render_to_string
|
2017-04-23 20:04:58 +00:00
|
|
|
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,
|
|
|
|
subject,
|
2017-05-21 19:14:06 +00:00
|
|
|
to_recipients=[],
|
2017-05-21 18:13:49 +00:00
|
|
|
cc_recipients=[],
|
|
|
|
bcc_recipients=[],
|
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-30 09:32:49 +00:00
|
|
|
attachment=None,
|
2017-04-23 20:04:58 +00:00
|
|
|
attachment_filename=''
|
2017-04-18 18:46:57 +00:00
|
|
|
):
|
2017-05-21 18:13:49 +00:00
|
|
|
if not isinstance(to_recipients, list):
|
|
|
|
to_recipients = [to_recipients]
|
2017-04-21 22:23:12 +00:00
|
|
|
|
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-05-21 18:13:49 +00:00
|
|
|
to_recipients,
|
2017-05-21 20:11:54 +00:00
|
|
|
bcc_recipients + [settings.ARCHIVE_EMAIL] if bcc_recipients else [settings.ARCHIVE_EMAIL],
|
2017-05-21 18:13:49 +00:00
|
|
|
cc_recipients
|
2017-04-18 18:46:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
2017-04-30 09:32:49 +00:00
|
|
|
try:
|
|
|
|
msg.send(fail_silently=False)
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception('exception while sending email: {}'.format(e))
|
|
|
|
return False
|
2017-04-18 18:46:57 +00:00
|
|
|
|
|
|
|
return True
|
2017-04-23 20:04:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_outgoing_email(
|
|
|
|
text_template,
|
|
|
|
formatdict,
|
|
|
|
subject,
|
2017-05-21 19:14:06 +00:00
|
|
|
to_recipients=[],
|
2017-05-21 18:13:49 +00:00
|
|
|
cc_recipients=[],
|
|
|
|
bcc_recipients=[],
|
2017-04-23 20:04:58 +00:00
|
|
|
html_template='',
|
|
|
|
sender='BornHack <info@bornhack.dk>',
|
2017-04-30 09:32:49 +00:00
|
|
|
attachment=None,
|
2017-04-23 20:04:58 +00:00
|
|
|
attachment_filename=''
|
|
|
|
):
|
|
|
|
""" adds an email to the outgoing queue
|
2017-05-21 15:23:48 +00:00
|
|
|
recipients is a list of to recipients
|
2017-04-23 20:04:58 +00:00
|
|
|
"""
|
|
|
|
text_template = render_to_string(text_template, formatdict)
|
|
|
|
|
|
|
|
if html_template:
|
|
|
|
html_template = render_to_string(html_template, formatdict)
|
|
|
|
|
2017-05-21 18:13:49 +00:00
|
|
|
if not isinstance(to_recipients, list):
|
|
|
|
to_recipients = [to_recipients]
|
2017-05-21 15:23:48 +00:00
|
|
|
|
2017-05-21 18:13:49 +00:00
|
|
|
for recipient in to_recipients:
|
2017-05-21 13:04:51 +00:00
|
|
|
try:
|
2017-05-21 15:23:48 +00:00
|
|
|
validate_email(recipient)
|
2017-05-21 13:04:51 +00:00
|
|
|
except ValidationError:
|
|
|
|
return False
|
2017-04-23 20:04:58 +00:00
|
|
|
|
2017-04-30 09:32:49 +00:00
|
|
|
email = OutgoingEmail.objects.create(
|
2017-04-23 20:04:58 +00:00
|
|
|
text_template=text_template,
|
|
|
|
html_template=html_template,
|
|
|
|
subject=subject,
|
|
|
|
sender=sender,
|
2017-05-21 18:13:49 +00:00
|
|
|
to_recipients=to_recipients,
|
|
|
|
cc_recipients=cc_recipients,
|
|
|
|
bcc_recipients=bcc_recipients
|
2017-04-23 20:04:58 +00:00
|
|
|
)
|
2017-04-30 09:32:49 +00:00
|
|
|
|
|
|
|
if attachment:
|
|
|
|
django_file = ContentFile(attachment)
|
|
|
|
django_file.name = attachment_filename
|
|
|
|
email.attachment.save(attachment_filename, django_file, save=True)
|
|
|
|
|
2017-04-23 20:04:58 +00:00
|
|
|
return True
|