symposion_app/symposion/utils/mail.py

32 lines
983 B
Python
Raw Normal View History

2012-07-12 05:20:55 +00:00
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.contrib.sites.models import Site
def send_email(to, kind, **kwargs):
2014-07-30 18:19:26 +00:00
2012-07-12 05:20:55 +00:00
current_site = Site.objects.get_current()
2014-07-30 18:19:26 +00:00
2012-07-12 05:20:55 +00:00
ctx = {
"current_site": current_site,
"STATIC_URL": settings.STATIC_URL,
}
ctx.update(kwargs.get("context", {}))
subject = "[%s] %s" % (
current_site.name,
render_to_string("symposion/emails/%s/subject.txt" % kind, ctx).strip()
2012-07-12 05:20:55 +00:00
)
2014-07-30 18:19:26 +00:00
message_html = render_to_string("symposion/emails/%s/message.html" % kind, ctx)
2012-07-12 05:20:55 +00:00
message_plaintext = strip_tags(message_html)
2014-07-30 18:19:26 +00:00
2012-07-12 05:20:55 +00:00
from_email = settings.DEFAULT_FROM_EMAIL
2016-07-01 00:10:23 +00:00
bcc_email = settings.ENVELOPE_BCC_LIST
2014-07-30 18:19:26 +00:00
2016-07-01 00:10:23 +00:00
email = EmailMultiAlternatives(subject, message_plaintext, from_email, to, bcc=bcc_email)
2012-07-12 05:20:55 +00:00
email.attach_alternative(message_html, "text/html")
email.send()