我遇到了有关以下脚本的问题。
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: ' + "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f286978186b286978186dc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>" + '\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
}
})
};
}
问题在于,德语中一些重要的特殊字符(“ä、ö、ü ...”)未正确传输到生成的草稿电子邮件中。
在草稿电子邮件中,字符“ö”被转换为“ö”。主题和正文是一样的。
如何告诉 Google Apps 脚本创建正确的字符?
最佳答案
以下是通过将短语德语 (“ä, ö, ü ...”)
邮寄给我自己而收到的原始电子邮件正文示例:
MIME-Version: 1.0
Reply-To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="731e1633160b121e031f165d101c1e" rel="noreferrer noopener nofollow">[email protected]</a>
Sender: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05686045607d64687569602b666a68" rel="noreferrer noopener nofollow">[email protected]</a>
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: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cca1a98ca9b4ada1bca0a9e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>
X-Google-Sender-Auth: F53iraIGVkzcvVsK_xQusL1FB9Q
Message-ID: <CADiaKr4qDq8Anp4C0BymXC411nc0LtcVMrhTrQ6qErO00hB4CQ@mail.gmail.com>
Subject: german
From:Me <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f020a2f0a170e021f030a410c0002" rel="noreferrer noopener nofollow">[email protected]</a>>
To: Me <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8de0e8cde8f5ece0fde1e8a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>>
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',Helvetica,sans-serif;font-size:15px;line-height:19.5=
px">=C2=A0German language ("=C3=A4, =C3=B6, =C3=BC ...")</span><b=
r></div></div>
--94eb2c06bef09d6d1c0527750dfe--
与您的代码相比,您似乎忘记在消息中设置字符集
,因此默认为 US-ASCII。
您需要与读者的电子邮件客户端沟通,了解他们需要使用什么编码来解释消息,这是作为电子邮件相关部分的内容类型元数据的一部分完成的。
类似这样的事情:
var raw =
'Subject: ' + subject + '\n' +
'To: ' + "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="562233252216223325227835393b" rel="noreferrer noopener nofollow">[email protected]</a>" + '\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';
假设您只发送纯文本电子邮件。如果您还发送 HTML 部分,则它还需要自己的 Content-Type
元标记,如果您需要替代编码,该元标记可能会有所不同。
关于google-apps-script - 在 Google Apps 脚本中创建电子邮件 - 无法创建 ä、ö、ü 等特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401719/