Q:

Sending an email using sendgrid in a Django project using an HTML template

I am trying to send an email from my Django project using my Sendgrid account. For now, I am sending the email in the following way:
import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = Email("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

The code is taken from this link:
https://github.com/sendgrid/sendgrid-python
Now, what I want to do this in the content section, instead of doing a text/plain email, I want to send a nicely formatted HTML email. Preferably, I want some way to reference a template and pass in a context. Is there any way to do this? Thanks.

A:

This should be pretty easy. You'll want to use render_to_string. It takes a request, template and context dictionary just like render but produces a string. Then pass it to Content and set the mime type to text\html.

