Requests to send mail with both plain text and HTML content fail if the HTML content is specified first.
sg = sendgrid.SendGridAPIClient(apikey=sendgrid_key)
mail = Mail()
from_email = Email("[email protected]")
to_email = Email("[email protected]")
subject = "Sending with SendGrid is Fun"
per = Personalization()
mail.from_email = from_email
mail.subject = subject
html_content = Content("text/html", "<html><body>some text here</body></html>")
plain_content = Content("text/plain", "and easy to do anywhere, even with Python")
### Add plain content first
mail.add_content(plain_content)
### Add HTML content next
mail.add_content(html_content)
per.add_to(to_email)
mail.add_personalization(per)
response = sg.client.mail.send.post(request_body=mail.get())
The library should sort content into the order that the API expects. (I'm not clear why the order should matter to the API鈥攑erhaps this should be fixed there instead.)
@hugomallinson Interesting find - I hope I fixed that issue in this Pull Request
Very interesting. In addition to @dsouzarc's fix, I think this should be either changed in the API or mentioned in the docs.
It should probably be in the API otherwise we'd have to implement this fix in every client/SDK, but I wonder what's causing the issue.
Most helpful comment
@hugomallinson Interesting find - I hope I fixed that issue in this Pull Request