sendgrid-java: Problem with UTF-8 characters in substitutional tags
Subject Line is not being encoded correctly for characters outside the ASCII range. When sending emails, none of the subjects I specify with special characters (through ether the API with mail.setSubject or through the email templates) are presented correctly when using content type text/html and no substitution tags in the subject. Am I missing something that I need to specify in order for this to work as expected?
Example:

The body doesn’t seem to be affected by this issue. Right now I am specifying the content type of an email by using:
Content("text/html", "some text here");
This seems to default to the UTF-8 charset conversion for the BODY which is fine, however it doesn’t seem to affect the SUBJECT line unless I add a Substitution Tag to it or change the content type. For example:
mail.setSubject("¡Feliz Cumpleaños!"); <---- Fails to do conversion. See the example above.
mail.setSubject("¡%Happy_Spanish% Cumpleaños!"); <---- Displayed correctly
Content("text/plain", "some text here"); <----- Also presents the subject line correctly. (Regardless of the presence of any substitution tags).
Note: I haven’t tested the other content types.
Some web application such as Mailinator and Gmail would do the UTF-8 MIME content transfer encoding regardless of whether or not it was specified as a content-type. This is why only on these Web Applications the subject will display correctly.
So in summary: Using content text/html and a subject without any substitution tags, would fail to present special characters in the subject line correctly or rather fail to apply MIME content transfer encoding for UTF-8 characters.
If I understand the issue correctly, an email subject is sent as a header which requires ASCII only characters, which means it needs to be encoded using RFC 2047 specs using Quoted-Printable or Base64 such that:
=?charset?encoding?encoded-text?=
e.g.
=?utf-8?Q?hello?=
I could probably temporarily solve this by using Java and pre-encoding the subject before sent http://stackoverflow.com/questions/3451256/javamail-changing-charset-of-subject-line However this would not work for subjects specified in transactional templates using the Web Application. Which is the way we wanted to use it. Marketing personnel is creating and updating templates that we developers use by specifying the template id (which pulls the subject line from it as well) using the Java API.
Steps to Reproduce
- Post JSON request with special characters using API
e.g.:
{ "from" : { "name" : "Joaquin Pockels", "email" : "email.test2@email.com" }, "subject" : "Default Subject Line", "personalizations" : [ { "to" : [ { "email" : "email.test1@email.com" } ], "substitutions" : { "%Today_Date_Spanish__c%" : "11 DE AGOSTO DE 2016", }, "custom_args" : { "sf_contact_id" : "xxxxxxxxxxx" }, "subject": "¡Feliz Cumpleaños!" } ], "content" : [ { "type" : "text/html", "value" : "some text here" } ] }
Technical details:
- sendgrid-java Version: master (latest commit: [commit number])
- Java Version: 3.0.8
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 3
- Comments: 15 (8 by maintainers)
Hi @thinkingserious ,
As billing was not enabled for my Google App Engine project, my emails have not been sent. Now the application works very well in GAE with transactional templates and attachments with this jar https://github.com/sendgrid/sendgrid-java. Thanks for the response. But it has a problem on UTF-8 characters in substitutional tags. Sorry for the hassle created earlier. Thanks for the support.
@armis-tech,
Thanks for the follow up. I’ve added this to our backlog for further investigation.
Hello @thinkingserious it works with the latest jar. Thank you
ok sure thanks. I will check and let you know.