bornhack-website/src/utils/email.py

119 lines
3.2 KiB
Python
Raw Normal View History

import logging
import magic
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile
from django.core.mail import EmailMultiAlternatives
from django.core.validators import validate_email
from django.template.loader import render_to_string
from .models import OutgoingEmail
2019-06-16 12:32:24 +00:00
logger = logging.getLogger("bornhack.%s" % __name__)
def _send_email(
text_template,
subject,
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:33:56 +00:00
attachment=None,
attachment_filename="",
):
2017-05-21 18:13:49 +00:00
if not isinstance(to_recipients, list):
to_recipients = [to_recipients]
try:
# put the basic email together
msg = EmailMultiAlternatives(
subject,
text_template,
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,
)
# 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")
# is there an attachment to this mail?
2019-08-08 08:28:02 +00:00
if attachment:
# figure out the mimetype
mimetype = magic.from_buffer(attachment, mime=True)
msg.attach(attachment_filename, attachment, mimetype)
except Exception as e:
2019-06-16 12:32:24 +00:00
logger.exception("exception while rendering email: {}".format(e))
return False
# send the email
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))
return False
return True
def add_outgoing_email(
text_template,
formatdict,
subject,
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>",
attachment=None,
2019-06-16 12:32:24 +00:00
attachment_filename="",
responsible_team=None,
hold=False,
):
"""adds an email to the outgoing queue
recipients is a list of to recipients
"""
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]
# loop over recipients and validate each
for recipient in to_recipients + cc_recipients + bcc_recipients:
try:
validate_email(recipient)
except ValidationError:
logger.error(
f"There was a problem validating the email {recipient} - returning False"
)
return False
email = OutgoingEmail.objects.create(
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,
hold=hold,
responsible_team=responsible_team,
)
2019-08-08 09:09:25 +00:00
if attachment:
django_file = ContentFile(attachment)
django_file.name = attachment_filename
email.attachment.save(attachment_filename, django_file, save=True)
return True