Encoding for special characters not working in email subject for sendgrid templates

10.3k Views Asked by At

I have been using the sendgrid-ruby gem for sending emails. The subject of the email doesn't decode special characters properly. Eg. Sending this subject for the email How's it going translates to this in the actual email How's it going

I have tried encoding the string for subject to different formats such as ASCII, ISO_8859_1 but none of this works.

@body_json['personalizations'][0]['dynamic_template_data'] = {
    'email_title': @email_title,
    'content': @description,
    'subject': "How's it going"
}

SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client.mail._('send').post(request_body: @body_json)

The subject for email should show special characters correctly such as ' & :

2

There are 2 best solutions below

3
On BEST ANSWER

You should be using triple brackets in your subject section i.e. {{{subject}}} for subjects with special characters. If you use the double brackets approach your string is going to be HTML encoded.

Check this link from SendGrid repository https://github.com/sendgrid/sendgrid-nodejs/issues/741#issuecomment-422026634

1
On

Okay so after chatting with sendgrid support I was able to figure this out. The issue is not with the sendgrid request from my side. Whenever making a template always be sure that subject title in header is inside double brackets i.e. {{subject}}. This will ensure that all special characters work inside this block.