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
|
2018-08-30 15:32:24 +00:00
|
|
|
import logging, magic
|
2019-06-16 12:32:24 +00:00
|
|
|
|
2017-04-18 18:46:57 +00:00
|
|
|
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=[],
|
2019-06-16 12:32:24 +00:00
|
|
|
html_template="",
|
|
|
|
sender="BornHack <info@bornhack.dk>",
|
2019-08-07 21:25:52 +00:00
|
|
|
attachments=(),
|
2019-06-16 12:32:24 +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,
|
2019-06-16 12:32:24 +00:00
|
|
|
bcc_recipients + [settings.ARCHIVE_EMAIL]
|
|
|
|
if bcc_recipients
|
|
|
|
else [settings.ARCHIVE_EMAIL],
|
|
|
|
cc_recipients,
|
2017-04-18 18:46:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# is there a html version of this email?
|
|
|
|
if html_template:
|
2019-06-16 12:32:24 +00:00
|
|
|
msg.attach_alternative(html_template, "text/html")
|
2017-04-18 18:46:57 +00:00
|
|
|
|
2018-08-30 15:32:24 +00:00
|
|
|
# is there an attachment to this mail?
|
2019-08-07 21:25:52 +00:00
|
|
|
if attachments:
|
|
|
|
for attachment in attachments:
|
|
|
|
# figure out the mimetype
|
|
|
|
mimetype = magic.from_buffer(attachment, mime=True)
|
|
|
|
msg.attach(attachment_filename, attachment, mimetype)
|
2017-04-18 18:46:57 +00:00
|
|
|
except Exception as e:
|
2019-06-16 12:32:24 +00:00
|
|
|
logger.exception("exception while rendering email: {}".format(e))
|
2017-04-18 18:46:57 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# send the email
|
2017-04-30 09:32:49 +00:00
|
|
|
try:
|
|
|
|
msg.send(fail_silently=False)
|
|
|
|
except Exception as e:
|
2019-06-16 12:32:24 +00:00
|
|
|
logger.exception("exception while sending email: {}".format(e))
|
2017-04-30 09:32:49 +00:00
|
|
|
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=[],
|
2019-06-16 12:32:24 +00:00
|
|
|
html_template="",
|
|
|
|
sender="BornHack <info@bornhack.dk>",
|
2017-04-30 09:32:49 +00:00
|
|
|
attachment=None,
|
2019-06-16 12:32:24 +00:00
|
|
|
attachment_filename="",
|
2017-04-23 20:04:58 +00:00
|
|
|
):
|
|
|
|
""" 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,
|
2019-06-16 12:32:24 +00:00
|
|
|
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
|