SendGrid email displayed as plaintext instead of HTML

Viewed 730

I am sending a dynamic email template through SendGrid using python. The correct HTML email template is displayed in inbox when I test the email through Sendgrid UI, but a plaintext version is displayed when the email is sent via python . Here's the code:

import sendgrid
import os
from sendgrid.helpers.mail import *
from sendgrid import SendGridAPIClient

sg = sendgrid.SendGridAPIClient('MY_KEY_HERE')

from_email = Email("example@email.com")
to_email = To("example@email.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)
mail.template_id = "d-9e2812d070bb458e8d6cbd6c9b8922f6"
mail.dynamic_template_data = {
  "first_name": "John",
  "last_name": "Blacutt",
  }
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

Here's the plaintext email:

enter image description here

1 Answers

If you are using a stored template and you want your dynamic content to render as HTML, you need to add an extra set of handlebars in the template itself else it will render as plain text.

EX:

  <p>
    {{{MailBody}}}
  </p>

Notice the extra handlebars around {{MailBody}}

Related