Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. def send_templated_email(subject, email_template_name, email_context, recipients,
  2. sender=None, bcc=None, reply_to=None, fail_silently=True, files=None):
  3.  
  4. from django.core.mail import EmailMultiAlternatives
  5. from django.template import loader, Context
  6. from django.utils.html import strip_tags
  7.  
  8. c = Context(email_context)
  9. if not sender:
  10. sender = settings.DEFAULT_FROM_EMAIL
  11.  
  12. template = loader.get_template(email_template_name)
  13.  
  14. text_part = strip_tags(template.render(c))
  15. html_part = template.render(c)
  16.  
  17. if reply_to:
  18. headers = {'Reply-To': reply_to}
  19. else:
  20. headers = {}
  21.  
  22. if type(recipients) == str:
  23. if recipients.find(','):
  24. recipients = recipients.split(',')
  25. elif type(recipients) != list:
  26. recipients = [recipients,]
  27.  
  28. msg = EmailMultiAlternatives(subject,
  29. text_part,
  30. sender,
  31. recipients,
  32. bcc=bcc,
  33. headers=headers)
  34. # if files:
  35. # if type(files) != list:
  36. # files = [files,]
  37.  
  38. # for f in files:
  39. # msg.attach_file(f)
  40.  
  41. if files:
  42. msg.attach(files.name, files.read(), files.content_type)
  43.  
  44. msg.attach_alternative(html_part, "text/html")
  45.  
  46. return msg.send(fail_silently)
  47.  
  48. msg.attach(files.name, files.read(), files.content_type)
  49.  
  50. JVBERi0xLjcKJeLjz9MKOCAwIG9iago8PAovRmlsdGVyIC9GbGF0ZURlY29kZQovTiAzCi9BbHRlcm5hdGUgL0RldmljZVJHQgovTGVuZ3RoIDI1OTMKPj4Kc3RyZWFtCnicnZZ3VFTXFofPvXd6oc0w dBh6r1IGEOkdpFdRGGYGG....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement