如何使用Google Apps脚本在电子邮件主题中使用Emoji? [英] How to use Emoji in email subject using Google Apps Script?

查看:10
本文介绍了如何使用Google Apps脚本在电子邮件主题中使用Emoji?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

GmailApp.sendEmail(
  'abc@gmail.com', 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。对于我的as other direction, in your goal, can you use Gmail API?问题,您回答了Yes, Gmail API will work.。所以我知道您的目标可以使用Gmail API来实现。

修改点:

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

当上述要点反映到脚本中时,它将如下所示。

示例脚本:

在使用此脚本之前,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("
");
  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使用带有主题的emoji时,您也可以使用以下脚本。但是,在这种情况下,在我的环境中,当表情符号包含在文本正文和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"
      );
    

引用:

这篇关于如何使用Google Apps脚本在电子邮件主题中使用Emoji?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆