开发者

rendering html templates through mails sent through django

I have a django template which I have to send as email body, the variables that i am sending are getting rendered in the email accordingly but the html tags used in the template are not rendering.开发者_运维百科Here is the code..

t = loader.get_template('registration/subscription_employee.html')
                c = {'site_name': current_site.domain,'user': user,'employer':employer}
                send_mail(("Subscription Agreement of %s")%data['username'], t.render(Context(c)), None, [settings.PAYPAL_PRIMARY_EMAIL,data['email']],fail_silently = True) 

how can i ensure that html tags are also rendered accordingly.


By default, django sends text/plain emails, so you won't see any HTML in your letters.

You can change this, take a look at the documentation

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()


You either need to use attach_alternative() or specify a content sub type for your message. See the docs on sending HTML emails: http://docs.djangoproject.com/en/1.3/topics/email/. The HTML-specific info is about halfway down the page. Hope that helps you out.


You're sending HTML message as plain text email. If you want to send HTML only, use EmailMessage and set add header {'Content-type': 'text/html'}. Much user friendlier solution would be to send both text and HTML with EmailMultiAlternatives.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜