Created
December 19, 2013 10:36
-
-
Save zhenyi2697/8037328 to your computer and use it in GitHub Desktop.
Django: send email from template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.core.mail import EmailMultiAlternatives | |
| from django.template.loader import render_to_string | |
| from django.utils.html import strip_tags | |
| subject, from_email, to = 'Order Confirmation', 'admin@yourdomain.com', 'someone@somewhere.com' | |
| html_content = render_to_string('the_template.html', {'varname':'value'}) # ... | |
| text_content = strip_tags(html_content) # this strips the html, so people will have the text as well. | |
| # create the email, and attach the HTML version as well. | |
| msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) | |
| msg.attach_alternative(html_content, "text/html") | |
| msg.send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment