Sendgrid-python: text/plain must precede text/html content

Created on 27 Oct 2017  路  3Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

Requests to send mail with both plain text and HTML content fail if the HTML content is specified first.

Code

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())

Steps to Reproduce

  1. The above code works properly, but if you reverse the order of the add_content lines, http-client throws a BadRequestsError

Expected Result

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.)

Technical details:

  • sendgrid-python Version: master (latest commit: [b12728a53d4c997832c56289c7559f22acf1ff90])
  • Python Version: 2.7.13
medium hacktoberfest work in progress community enhancement

Most helpful comment

@hugomallinson Interesting find - I hope I fixed that issue in this Pull Request

All 3 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thinkingserious picture thinkingserious  路  3Comments

MadReal picture MadReal  路  4Comments

thinkingserious picture thinkingserious  路  5Comments

inputjoker picture inputjoker  路  4Comments

cl1ent picture cl1ent  路  5Comments