Sign In Register

How can we help you today?

Start a new topic
Answered

character encoding problem with getHttp postForm on mailgun

Hi,


In our project we have cloud code that sends emails to players in french through mailgun. It's working fine in english but in french the resulting emails are badly encoded. Every accentuated characters are replaced with �. I've tried to set content-type header to utf-8 , use postJson instead of postForm with no luck. Do you know what is the cause of the problem?  

here's a simplify version of our code cloud code:

 

var form = {
	"to":"jdoe@mycompany.com", 
	"from":"cgauthier@mycompany.com",
	"fromname":"John Doe",
	"subject": "Probl\u00e8me avec les \u00e0",
	"text":"\u00e9l\u00e8ve"
};

var httpSender = Spark.getHttp("https://api.mailgun.net/v3/mg.mycompany.com/messages");
httpSender.setBasicAuth("api","key-9x9x9x9x9x9x9x9x9");

var response = httpSender.postForm(form);
var responseCode = response.getResponseCode();
if (responseCode !== 200){
    Spark.setScriptError("error", "Send Email failed, error code: " + responseCode + " : " + response.getResponseString());
}
else {
    Spark.setScriptData("data", "success");
}

 

Expected result:

Subject: Problème avec les à

Body: élève


Actual result:

Subject: Probl�me avec les �

Body: �l�ve



Best Answer

Hi Christian,


This was resolved in the ticket system but I thought it would be worth sharing the solution for anyone who may run into the same issue as you.


"SendGrid is sending the email with UTF-8 encoding - therefore you will need to encode your subject and body using UTF-8.

I've included a snippet of code below which works for us - We've tested this sending to Outlook and GMail.



function encode_utf8(s) {

  return unescape(encodeURIComponent(s));

}


mySendGrid.setSubject(encode_utf8("Problème avec les à"));

mySendGrid.setText(encode_utf8("Problème avec les à"));"


Thanks,

Liam


Hi Christian 


You need to add some parameters to the url to make this work. You can set both characterEncoding and contentType.

characterEncoding=UTF-8&contentType=application/json


Then UTF-8 characters will be supported.


Thanks,


Oisin

I've tried with the extra parameters and it's not working. By the way i'm not posting json, i'm using postForm() not postJson()

Using postman, it's working great. Here's the request outputed in Curl by Postman:

curl -X POST -H "Authorization: Basic B----------------------------U" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "to=cgauthier@mycompany.com" -F "from=cgauthier@mycompany.com" -F "fromname=Christian Gauthier" -F "subject=Problème avec les à" -F "text=élève" "https://api.mailgun.net/v3/mg.teamupmobileapp.com/messages"

So here's what I tried so far and nothing's working:

A) var httpSender = Spark.getHttp("https://api.mailgun.net/v3/mg.teamupmobileapp.com/messages?characterEncoding=UTF-8&contentType=application/json");

  email still has

B) var httpSender = Spark.getHttp("https://api.mailgun.net/v3/mg.teamupmobileapp.com/messages?characterEncoding=UTF-8");

  email still has

C) httpSender.setHeaders({"content-type": "application/json; charset='utf-8'"});

  email still has

 

D) httpSender.setHeaders({"content-type": "charset='utf-8'"});

  mailgun returns an error: from parameter is missing

E) httpSender.setHeaders({"content-type": "form-data; charset='utf-8'"});

  mailgun returns an error: from parameter is missing

F) httpSender.setHeaders({"Content-Disposition": "form-data; charset='utf-8'"});

  email still has

I'm out of idea :-(

Hello guys,


I know this is a tough one :-) but do you have any hints on this one?

Hey Christian,
Have you tried this on any other browsers to see if the output is different?

Sean

 

While searching for solution I tried your built-in sendGrid solution and I discovered an interesing problem. Accent (é for example) entered through your web site interface (NoSQL, Test harness or Cloud Code editor) are not encoded in a way that outlook likes (no problem with gmail)


The fist image show 2 documents:

code:"TEST0" was added using an UTF8 encoded json file and a REST Api call

code:"TEST1" was added using the browser NoSQL interface


If you run the following code (notice the subject has an é that will also be badly encoded since it's was entered in the "cloud code" interface):


 

var msg0 = Spark.metaCollection("EmailDebugging").findOne({"code":"TEST0"}).value;
var msg1 = Spark.metaCollection("EmailDebugging").findOne({"code":"TEST1"}).value;

var mySendGrid = Spark.sendGrid("xxxxxxx", "yyyyyyyyyyy");
mySendGrid.setFrom("notimportant@somewhere.com", "Tester");
mySendGrid.addTo("anyhotmailrecipient@hotmail.com","Your Name");
mySendGrid.setSubject("This é will fail");
mySendGrid.setText(msg0 + "-" + msg1);
var response = mySendGrid.send(); 
response = JSON.parse(response); 
if (response["message"] !== "success"){
    Spark.getLog().error("Send Email fail: " + response.message);
}

 While searching for solution I tried your built-in sendGrid solution and I discovered an interesing problem. Accent (é for example) entered through your web site interface (NoSQL, Test harness or Cloud Code editor) are not encoded in a way that outlook likes (no problem with gmail)

The fist image show 2 documents:

code:"TEST0" was added using an UTF8 encoded json file and a REST Api call

code:"TEST1" was added using the browser NoSQL interface

If you run the following code (notice the subject has an é that will also be badly encoded since it's was entered in the "cloud code" interface):

The resulting email from outlook showing the encoding problem :



Answer

Hi Christian,


This was resolved in the ticket system but I thought it would be worth sharing the solution for anyone who may run into the same issue as you.


"SendGrid is sending the email with UTF-8 encoding - therefore you will need to encode your subject and body using UTF-8.

I've included a snippet of code below which works for us - We've tested this sending to Outlook and GMail.



function encode_utf8(s) {

  return unescape(encodeURIComponent(s));

}


mySendGrid.setSubject(encode_utf8("Problème avec les à"));

mySendGrid.setText(encode_utf8("Problème avec les à"));"


Thanks,

Liam

Login to post a comment