Sendgrid-python: Apostrophe in Subject Not Parsing Properly

Created on 13 Aug 2018  路  6Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

I've been sending subjects with apostrophes and quotation marks and they've appeared with HTML encoding: " and '

I've tried encoding it straight strign, with utf_8 encoding, and with utf_8 encoding then decoded for unicode_escape.

When tried in the API explorer it worked fine, with the characters appearing as they should.

An example: subject = "Test: \" Charle's - James\'s - chr: " + chr(39) is received as:
Test: " Charle's - James's - chr: '

Steps to Reproduce

import sendgrid
from sendgrid.helpers.mail import *

SENDGRID_API_KEY = 'XXX'
sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
OUTGOING_EMAIL = '[email protected]'
OUTGOING_EMAIL_NAME = 'Mr Example'
SENDGRID_TEMPLATE_ID = 'TEMPLATE'

def sendEmail(to_email, subject, content):
    """ Sends email via sendgrid """
    data = {
        "personalizations": [
            {
                "to": [
                    {
                        "email": to_email
                    }
                ],
                "dynamic_template_data": {
                    "subject": subject,
                    "content": content
                },
            }
        ],
        "from": {
            "email": OUTGOING_EMAIL,
            "name": OUTGOING_EMAIL_NAME
        },
        "reply_to": {
            "email": "[email protected]",
            "name": "No Reply"
        },
        "template_id": SENDGRID_TEMPLATE_ID
    }

    response = sg.client.mail.send.post(request_body=data)
    print(response.status_code)
    print(response.body)
    print(response.headers)


# Test content goes here
to_email = '[email protected]'
subject = "Test: \" Charle's - James\'s - chr: " + chr(39)
subject_utf8 = subject.encode("utf_8").decode("unicode_escape")
content = ["Just testing out apostrophes", "Without escape: ' here", 'With esacpe: \' here']

sendEmail(to_email, subject, content)
sendEmail(to_email, subject_utf8, content)

Technical details:

  • sendgrid-python Version: 5.4.1
  • Python Version: 2.7.10
medium help wanted help wanted bug up for grabs up-for-grabs

Most helpful comment

Hey just wanted to update this, the issue was fixed but there is still some user action that needs to be taken.

You need to triple curly brace your subject variable for it to parse as text instead of HTML so use something like {{{subject}}}

Thank you Chewie at Sendgrid support for letting me know about this.

All 6 comments

Update:

After further testing, I've found this appears to be an issue with dynamic_template_data's interpretation of these characters, as passing in the subject line in normally in personalization works fine (see example below).

data = {
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]"
        }
      ],
      "subject": "Sending with SendGrid is' \" Fun"
    }
  ],
  "from": {
    "email": "[email protected]"
  },
  "content": [
    {
      "type": "text/plain",
      "value": "and easy to do anywhere, even with Python"
    }
  ]
}

My subject is being passed in as a handlebars variable into the subject line of my transactional template. This seems to be causing the issue, as it appears both when sent via the Python client library and via the API explorer.

I'll keep looking for a solution to this issue, but any help would be appreciated.

Hello @JamesTheTerry,

This issue should now be resolved, there a fix pushed out server side.

Please re-open if there are any further issues.

Thanks!

With Best Regards,

Elmer

Hey just wanted to update this, the issue was fixed but there is still some user action that needs to be taken.

You need to triple curly brace your subject variable for it to parse as text instead of HTML so use something like {{{subject}}}

Thank you Chewie at Sendgrid support for letting me know about this.

Hey @JamesTheTerry, do you think you could add it to the docs real quick? So that others can benefit from this information!

Thanks a lot for your support!

@af4ro Probably not the most well written, but here you go: https://github.com/sendgrid/docs/pull/3878

This caught me out - thanks for adding the solution.

Was this page helpful?
0 / 5 - 0 ratings