ruby-on-railssendgridsendgrid-templatessendgrid-ruby

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


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


Solution

  • 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