def render_mail(subject, template_prefix, to_emails, context, bcc=None, cc=None, **kwargs):
from_email = DEFAULT_FROM_EMAIL
if not re.match(r'^\[\s*Tunga', subject):
subject = '{} {}'.format(EMAIL_SUBJECT_PREFIX, subject)
bodies = {}
for ext in ['html', 'txt']:
try:
template_name = '{0}.{1}'.format(template_prefix, ext)
bodies[ext] = render_to_string(template_name,
context).strip()
except TemplateDoesNotExist:
if ext == 'txt':
if 'html' in bodies:
# Compose text body from html
bodies[ext] = convert_to_text(bodies['html'])
else:
# We need at least one body
raise
if bodies:
msg = EmailMultiAlternatives(subject, bodies['txt'], from_email, to_emails, bcc=bcc, cc=cc)
if 'html' in bodies:
try:
html_body = render_to_string(
'tunga/email/base.html', dict(email_content=bodies['html'])
).strip()
except TemplateDoesNotExist:
html_body = bodies['html']
msg.attach_alternative(premailer.transform(html_body), 'text/html')
else:
raise TemplateDoesNotExist
return msg
评论列表
文章目录