Reputation: 6254
I want to mail a simple HTML file containing some data. I want the user to receive rendered HTML. I've tried this solution:
b = render_to_string('mail_body.html',{'name':request.POST['name']})
send_mail(email_subject,b,'[email protected]', ['[email protected]'],
fail_silently=False)
but I don't get rendered HTML in the mail body and the user can see all of the HTML tags!
Alternatively, when I use the following code I just get the path of my HTML file in the mail body:
params = {}
params['name'] = request.POST['name']
context = Context(params)
t = Template('mail_body.html')
send_mail(email_subject,t.render(context), '[email protected]',
['[email protected]'], fail_silently=False)
Upvotes: 2
Views: 1283
Reputation: 3230
You could try something like this:
from django.core.mail import EmailMultiAlternatives
body = render_to_string(template_name, locals(),
context_instance=RequestContext(request))
email = EmailMultiAlternatives(subject, body, mail_sender, [contact_request.email], headers={'Reply-To': '[email protected]'})
email.content_subtype='html'
email.send(fail_silently=False)
Hope this leads into the right direction. Just note that there are a lot of variables in here which come from my own programming.
Upvotes: 2
Reputation: 2069
The problem is that you need to tell the users mail client that you are sending them an HTML email, otherwise it will just show the HTML as if it were text.
The setting is called a MIME type, you can read more about how to set this (and send HTML emails in python) in the python user guides.
Upvotes: 1