2

I've got a problem concerning the following script.

function doGet() {
     //Create draft email
     var body = "Project ö";
     var subject = "Subject ö";

  var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope

  var raw = 
      'Subject: ' + subject + '\n' + 
      'To: ' + "[email protected]" + '\n' +
      'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\n' +
      '' + body + '\n' + 
      '--1234567890123456789012345678--\n';

       var draftBody = Utilities.base64Encode(raw, Utilities.Charset.UTF_8);


  var params = {method:"post",
                contentType: "application/json",
                headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
                muteHttpExceptions:true,
                payload:JSON.stringify({
                  "message": {
                    "raw": draftBody
                  }
                })
               };
}

The problem is that some special characters that are important in German language ("ä, ö, ü ...") are not transferred correctly to the generated draft email.

In the draft Email the character "ö" is converted to "ö". It's the same in subject and body.

How can I tell Google Apps Script to create the correct characters?

4
  • Check this other question: stackoverflow.com/questions/34278328/… Commented Dec 21, 2015 at 21:31
  • Try using Utilities.base64EncodeWebSafe instead. Commented Dec 21, 2015 at 23:30
  • base64EncodeWebSafe doesn't make a difference. Commented Dec 23, 2015 at 11:12
  • @Gerardo: You're right. It works like described. Thank you very much!!! Commented Dec 23, 2015 at 11:40

1 Answer 1

2

Here's an example of the raw email body received by mailing the phrase German language ("ä, ö, ü ...") to myself:

MIME-Version: 1.0
Reply-To: [email protected]
Sender: [email protected]
Received: by x.x.x.x with HTTP; Mon, 21 Dec 2015 20:29:36 -0800 (PST)
Date: Mon, 21 Dec 2015 23:29:36 -0500
Delivered-To: [email protected]
X-Google-Sender-Auth: F53iraIGVkzcvVsK_xQusL1FB9Q
Message-ID: <CADiaKr4qDq8Anp4C0BymXC411nc0LtcVMrhTrQ6qErO00hB4CQ@mail.gmail.com>
Subject: german
From:Me <[email protected]>
To: Me <[email protected]>
Content-Type: multipart/alternative; boundary=94eb2c06bef09d6d1c0527750dfe

--94eb2c06bef09d6d1c0527750dfe
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

 German language ("=C3=A4, =C3=B6, =C3=BC ...")

--94eb2c06bef09d6d1c0527750dfe
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:arial,he=
lvetica,sans-serif"><span style=3D"color:rgb(34,36,38);font-family:Arial,&#=
39;Helvetica Neue&#39;,Helvetica,sans-serif;font-size:15px;line-height:19.5=
px">=C2=A0German language (&quot;=C3=A4, =C3=B6, =C3=BC ...&quot;)</span><b=
r></div></div>

--94eb2c06bef09d6d1c0527750dfe--

Comparing that to your code, it appears that you've forgotten to set the charset in your message, so it's defaulting to US-ASCII.

You need to communicate to the reader's email client about what encoding they need to use to interpret the message, which is done as part of the Content-Type metadata of the relevant section of the email message.

Something like this:

var raw = 
      'Subject: ' + subject + '\n' + 
      'To: ' + "[email protected]" + '\n' +
      'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\n\n' +
      '--1234567890123456789012345678\n';
      'Content-Type: text/plain; charset=UTF-8\n' +
      'Content-Transfer-Encoding: quoted-printable\n' +
      '' + body + '\n' + 
      '--1234567890123456789012345678--\n';

That assumes you're only sending a plain text email. If you were also sending an HTML section, it would also require its own Content-Type meta-tag, which could be different if you required an alternative encoding.

Sign up to request clarification or add additional context in comments.

2 Comments

How/where should I set the charset?. I think I've set it in line: draftBody = Utilities.base64Encode(raw, Utilities.Charset.UTF_8);
You need to communicate to the reader's email client about what encoding they need to use to interpret the message, which is done as part of the Content-Type metadata of the relevant section of the email message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.