如何使用 Google Apps 脚本在电子邮件主题中使用表情符号?

Rav*_*lon 2 javascript gmail google-sheets google-apps-script emoji

我正在尝试使用 Google Apps 脚本发送电子邮件。

\n
// try 1\nconst subject = \'Hello World \';\n\n// try 2\nconst subject = \'Hello World \' + String.fromCodePoint(\'0x1F600\');\n\nGmailApp.sendEmail(\n  \'abc@gmail.com\', subject, \'\',\n  {htmlBody: \'<p>Hello World </p>\', name: \'ABC\'}\n);\n
Run Code Online (Sandbox Code Playgroud)\n

当我使用 \xe2\xad\x90 时,它在主题和 HTML 正文中都能完美运行。但是,当我使用 时,它在主题和 HTML 正文中显示带有问号的黑色菱形。

\n

我还检查了如何将表情符号插入使用 GmailApp 发送的电子邮件中?但它只展示了如何在电子邮件正文中使用它,而不是主题。

\n

我尝试过使用 MailApp 并且它有效,但由于某些原因我不想使用它。

\n

关于如何解决这个问题有什么想法吗?

\n

Tan*_*ike 5

我相信你的目标如下。

\n
    \n
  • \xee\x81\x9e\xe3\x83\xbb您想要使用 Google Apps 脚本发送包含主题中的表情符号的 Gmail 。
  • \n
  • GmailApp.sendEmail您想知道使用而不使用来实现目标的方法MailApp.sendEmail
  • \n
  • 但是,GmailApp.sendEmail使用时,主题可以包含表情符号。但表情符号不能在电子邮件正文中使用。所以我建议在这种情况下使用Gmail API。对于我的问题as other direction, in your goal, can you use Gmail API?,你回答了Yes, Gmail API will work.。所以我了解到您的目标可以使用 Gmail API 来实现。
  • \n
\n

修改要点:

\n
    \n
  • 在本例中,Gmail API 与高级 Google 服务一起使用。
  • \n
  • 而且,当您想要在电子邮件主题中包含表情符号时\xee\x81\x9e\xe3\x83\xbb,主题会作为 Base64 数据发送。\n
      \n
    • 当包含表情符号的值转换为base64数据时,在我的测试中,似乎需要将该值转换为base64为UFT-8。
    • \n
    • 我确认此解决方法也可用于GmailApp.sendEmail.
    • \n
    \n
  • \n
\n

当以上几点反映到脚本中时,就会变成如下所示。

\n

示例脚本:

\n

在使用此脚本之前,请在高级 Google 服务中启用 Gmail API。并且,请在函数中设置变量main()并运行该函数main()

\n
function convert(toEmail, fromEmail, name, subject, textBody, htmlBody) {\n  const boundary = "boundaryboundary";\n  const mailData = [\n    `MIME-Version: 1.0`,\n    `To: ${toEmail}`,\n    `From: "${name}" <${fromEmail}>`,\n    `Subject: =?UTF-8?B?${Utilities.base64Encode(subject, Utilities.Charset.UTF_8)}?=`,\n    `Content-Type: multipart/alternative; boundary=${boundary}`,\n    ``,\n    `--${boundary}`,\n    `Content-Type: text/plain; charset=UTF-8`,\n    ``,\n    textBody,\n    ``,\n    `--${boundary}`,\n    `Content-Type: text/html; charset=UTF-8`,\n    `Content-Transfer-Encoding: base64`,\n    ``,\n    Utilities.base64Encode(htmlBody, Utilities.Charset.UTF_8),\n    ``,\n    `--${boundary}--`,\n  ].join("\\r\\n");\n  return Utilities.base64EncodeWebSafe(mailData);\n}\n\n// Please run this function.\nfunction main() {\n  const toEmail = "###"; // Please set the email for `to`.\n  const fromEmail = "###"; // Please set the email for `from`.\n  const name = "ABC";\n  const subject = "Hello World \xee\x81\x9e\xe3\x83\xbb";\n  const textBody = "sample text body \xee\x81\x9e\xe3\x83\xbb";\n  const htmlBody = "<p>Hello World \xee\x81\x9e\xe3\x83\xbb</p>";\n  var raw = convert(toEmail, fromEmail, name, subject, textBody, htmlBody);\n  Gmail.Users.Messages.send({raw: raw}, "me");\n}\n
Run Code Online (Sandbox Code Playgroud)\n

笔记:

\n
    \n
  • 当您想使用带有主题的表情符号时GmailApp.sendEmail,还可以使用以下脚本。但是,在这种情况下,在我的环境中,当表情符号包含在文本正文和 HTML 正文中时,表情符号就看不到了。所以请小心这一点。

    \n
      const emailAddress = = "###"; // Please set the email for `to`.\n  const subject = \'Hello World \xee\x81\x9e\xe3\x83\xbb\';\n  GmailApp.sendEmail(\n    emailAddress,\n    `=?UTF-8?B?${Utilities.base64Encode(Utilities.newBlob(subject).getBytes())}?=`,\n    "sample text body"\n  );\n
    Run Code Online (Sandbox Code Playgroud)\n
  • \n
\n

参考:

\n\n

  • @TheMaster 当时,我可能已经检查了[这个](https://www.php.net/manual/en/function.mail.php#refsect1-function.mail-seealso)。现阶段,我认为你也可以使用[这个](https://www.sendblaster.com/utf8-email-subject-encoder/)。在这种情况下,“Hello World ⭐”将转换为“=?UTF-8?B?SGVsbG8gV29ybGQg8J+Yg+KtkA==?=”。当此编码值在 Gmail 中用作主题并发送时,您可以看到解码后的值。 (3认同)