GMail发送邮件时主题中包含的汉字无法正确显示

时间:2019-07-26 03:02:43

标签: gmail gmail-api

我通过GMail API使用javascript发送邮件。如果邮件主题为中文字符,则无法在“ GMail收件箱”中正确显示。

消息中有汉字没问题。

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, '_')
  }
});

1 个答案:

答案 0 :(得分:0)

我添加代码'=?UTF-8?B? ?='主题字符串,它可以工作。如下。

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";