9

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 ' & :

dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34
Abdul Haseeb
  • 370
  • 5
  • 14

2 Answers2

23

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

gbrennon
  • 899
  • 2
  • 11
  • 30
  • If you could notify that my answer is the correct one I would be happy :) – gbrennon Mar 09 '20 at 22:37
  • Using `{{{` (triple brackets) will prevent the HTML escaping of variables, which could eventually cause HTML injection security concern. Is there any other way to fix the above issue? – Arvind Kalra Jun 07 '21 at 07:41
  • hey @ArvindKalra i think securing what is going to be rendered on that place is our responsibility, so I would try to previously evaluate the contents of what is going to be rendered there – gbrennon Jun 08 '21 at 04:47
2

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.

Abdul Haseeb
  • 370
  • 5
  • 14
  • 1
    With double brackets, you won't get a special character to sanitize. You need to use the triple brackets approach. The correct answer is the one below – gbrennon Feb 19 '20 at 16:55