Search code examples
gmailgmail-api

Chinese Characters Included in Subject When GMail Sends Mail Can't Be Displayed Correctly


I send a mail in javascript via GMail API. If mail subject has Chinese Characters,it cant't display correctly in GMail Inbox.

And message has Chinese Characters no problem.

var email = '';
email =
    'Content-Type: multipart/mixed;boundary="foo_bar_baz"\r\n'+
    "To: " + $('#compose-to').val() + "\r\n" +
    "Subject: 这里是中文字符" +"\r\n\r\n";

email += "--foo_bar_baz\r\n" +
       "Content-Type: text/html;charset='UTF-8'\r\n" +
       "Content-Transfer-Encoding: 8bit\r\n";
email += "\r\n" + $('#compose-message').val()+"\r\n\r\n";

email += "--foo_bar_baz\r\n" +
    "Content-Type: application/pdf;attachment;filename=5678.pdf\r\n" +
    "Content-Transfer-Encoding: base64\r\n" +
    "Content-Disposition: attachment;filename=5678.pdf\r\n\r\n" +
    file_ + "\r\n\r\n";

var sendRequest = gapi.client.gmail.users.messages.send({
  'userId': 'me',
  'resource': {
    'raw': window.btoa(unescape(encodeURIComponent(email))).replace(/\+/g, '-').replace(/\//g, '_')
  }
});

Solution

  • I add code '=?UTF-8?B? ?=' to subject string and it works. As follows.

    var email = '';
    email =
        'Content-Type: multipart/mixed;boundary="foo_bar_baz"\r\n'+
        "To: " + $('#compose-to').val() + "\r\n" +
        "Subject: =?UTF-8?B?"++window.btoa(unescape(encodeURIComponent('这里是中文字 
        符')))+"?=" +"\r\n\r\n";