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

标签 javascript google-apps-script google-sheets gmail emoji

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

// try 1
const subject = 'Hello World 😃';

// try 2
const subject = 'Hello World ' + String.fromCodePoint('0x1F600');

GmailApp.sendEmail(
  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2746454467404a464e4b0944484a" rel="noreferrer noopener nofollow">[email protected]</a>', subject, '',
  {htmlBody: '<p>Hello World 😃</p>', name: 'ABC'}
);

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

我还检查过How to insert an emoji into an email sent with GmailApp?但它只展示了如何在电子邮件正文中使用它,而不是主题。

我尝试过使用 MailApp,它有效,但我不想将其用于 一些原因。

知道如何解决这个问题吗?

最佳答案

我相信您的目标如下。

  • 您想要使用 Google Apps 脚本发送主题中包含 ・ 等表情符号的 Gmail。
  • 您想了解使用 GmailApp.sendEmail 而不使用 MailApp.sendEmail 实现目标的方法。
  • 但是,当使用 GmailApp.sendEmail 时,主题可以包含表情符号。但表情符号不能在电子邮件正文中使用。所以我建议在这种情况下使用Gmail API。对于我的另一个方向的问题,在您的目标中,您可以使用 Gmail API 吗?,您回答是的,Gmail API 可以工作。。因此我了解到使用 Gmail API 可以实现您的目标。

修改点:

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

当以上几点反射(reflect)到脚本中时,就会变成如下。

示例脚本:

在使用此脚本之前,please enable Gmail API at Advanced Google services 。并且,请在函数 main() 中设置变量并运行函数 main()

function convert(toEmail, fromEmail, name, subject, textBody, htmlBody) {
  const boundary = "boundaryboundary";
  const mailData = [
    `MIME-Version: 1.0`,
    `To: ${toEmail}`,
    `From: "${name}" <${fromEmail}>`,
    `Subject: =?UTF-8?B?${Utilities.base64Encode(subject, Utilities.Charset.UTF_8)}?=`,
    `Content-Type: multipart/alternative; boundary=${boundary}`,
    ``,
    `--${boundary}`,
    `Content-Type: text/plain; charset=UTF-8`,
    ``,
    textBody,
    ``,
    `--${boundary}`,
    `Content-Type: text/html; charset=UTF-8`,
    `Content-Transfer-Encoding: base64`,
    ``,
    Utilities.base64Encode(htmlBody, Utilities.Charset.UTF_8),
    ``,
    `--${boundary}--`,
  ].join("\r\n");
  return Utilities.base64EncodeWebSafe(mailData);
}

// Please run this function.
function main() {
  const toEmail = "###"; // Please set the email for `to`.
  const fromEmail = "###"; // Please set the email for `from`.
  const name = "ABC";
  const subject = "Hello World ・";
  const textBody = "sample text body ・";
  const htmlBody = "<p>Hello World ・</p>";
  var raw = convert(toEmail, fromEmail, name, subject, textBody, htmlBody);
  Gmail.Users.Messages.send({raw: raw}, "me");
}

注意:

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

      const emailAddress = = "###"; // Please set the email for `to`.
      const subject = 'Hello World ・';
      GmailApp.sendEmail(
        emailAddress,
        `=?UTF-8?B?${Utilities.base64Encode(Utilities.newBlob(subject).getBytes())}?=`,
        "sample text body"
      );
    

引用文献:

关于javascript - 如何使用 Google Apps 脚本在电子邮件主题中使用表情符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66077675/

相关文章:

javascript - 这个时间戳脚本有什么问题?

javascript - 我应该如何解决 Chrome 的 XMLHttpRequest 侵入性 'loading' 行为?

javascript - 解析器错误/语法错误 : Unexpected token <

javascript - 如何将 flash 嵌入转换为 html5?

google-sheets - 更新行时获取电子邮件用户值

Excel - 将列中的值分成两列

电子表格 |工作表 : Display numbers between range in one cell

javascript - YouTube Javascript API 在 iPad iOS8 上无法按预期工作

google-app-engine - 从 Google Apps 脚本调用 Google Cloud Endpoint API 时出现错误 404

javascript - Google 脚本数字格式错误