首页 > 解决方案 > 如何在使用 GmailApp 发送的电子邮件中插入表情符号?

问题描述

我有一个发送自动电子邮件的 GAS 脚本,并希望包含几个表情符号。我试过使用简码和复制/粘贴,但到目前为止似乎没有任何效果。只是想看看我是否缺少任何东西。

编辑:这是代码:

var title = rowData.publicationTitle;
var journal = rowData.journalTitle;
var url = rowData.publicationUrl;

//Emoji goes here in the body:
var body = "Hi " + firstName + "!<br><br>I noticed your article <a href='" + url + "'>&ldquo;" + title + "&rdquo;</a> was recently published in <i>" + journal + "</i>. Congratulations! This is just a friendly reminder to please upload your original document and a PDF version to our publications app when you have time.<br><br>To upload your publication, you can <a href='http://support.cpes.vt.edu/publishing'>click here</a>.<br><br>Thanks!<br><br> CB<br><br><hr style='background-color: #d8d8d8; border: 0 none; color: #d8d8d8; height: 1px;'><span style='font-size:12px'><b>CPES Publications Reminders</b>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='mailto:leshutt@vt.edu' style='text-decoration:none'>Feedback</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='http://support.cpes.vt.edu/publishing' style='text-decoration:none;'>Publication uploads</a></span>";

var emailSubject = "Just a reminder to upload your article!";

var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();

if (emailStatus == "Pending" && emailData !== "No emails found") {
  GmailApp.sendEmail(email, emailSubject, body, {
    from: aliases[2],
    name: "CPES Bot",
    htmlBody: body
  });
}

我注意到发送星号(“⭐”)有效,但正常的笑脸(“”)显示为黑白、Unicode 风格的图标,我尝试过的其他所有内容都是问号。可以你只使用直到某个 Unicode 版本的表情符号?

标签: emailgoogle-apps-scriptgmailemoji

解决方案


您想发送一封带有 HTML 正文的电子邮件,包括表情符号。如果我的理解是正确的,那么这个修改呢?

关于 GmailApp 和 MailApp :

  • 不幸的是,GmailApp不能使用最近的表情符号字符。在GmailApp

    • 低于 Unicode 5.2 的表情符号可用于这种情况。
    • 超过 Unicode 6.0 的表情符号不能用于这种情况。
  • MailApp可以使用所有版本的表情符号。

“⭐”是 Unicode 5.1。但是“”是Unicode 6.0。这样,在您使用 GmailApp 的脚本中,您可以看到前者,但看不到后者。在 Michele Pisani 的示例脚本中,后者是使用 MailApp 发送的。所以性格没有被破坏。"" 是 Unicode 8.0。

修改点:

所以在你的脚本的情况下,修改点如下。

  • 使用MailApp而不是GmailApp.

或者

  • 使用 Gmail API。
    • 从您对 Michele Pisani 的评论来看,我担心这MailApp可能不适用于您的情况。所以我也想提出使用Gmail API的方法。

1.修改你的脚本

请进行如下修改。

从 :
GmailApp.sendEmail(email, emailSubject, body, {
至 :
MailApp.sendEmail(email, emailSubject, body, {

2. 使用 Gmail API

要使用此功能,请在高级 Google 服务和 API 控制台中启用 Gmail API,如下所示。

在高级 Google 服务中启用 Gmail API v1

  • 在脚本编辑器上
    • 资源 -> 高级 Google 服务
    • 打开 Gmail API v1

在 API 控制台启用 Gmail API

  • 在脚本编辑器上
    • 资源 -> 云平台项目
    • 查看 API 控制台
    • 在开始时,单击启用 API 并获取密钥等凭据。
    • 在左侧,单击库。
    • 在搜索 API 和服务中,输入“Gmail”。然后点击 Gmail API。
    • 单击启用按钮。
    • 如果 API 已经开启,请不要关闭。

如果现在您要使用使用 Gmail API 的脚本打开脚本编辑器,则可以通过访问此 URL https://console.cloud.google.com/apis/api/gmail.googleapis.com/为项目启用 Gmail API概述

示例脚本:

function convert(email, aliase, emailSubject, body) {
  body = Utilities.base64Encode(body, Utilities.Charset.UTF_8);
  var boundary = "boundaryboundary";
  var mailData = [
    "MIME-Version: 1.0",
    "To: " + email,
    "From: CPES Bot <" + aliase + ">",
    "Subject: " + emailSubject,
    "Content-Type: multipart/alternative; boundary=" + boundary,
    "",
    "--" + boundary,
    "Content-Type: text/plain; charset=UTF-8",
    "",
    body,
    "",
    "--" + boundary,
    "Content-Type: text/html; charset=UTF-8",
    "Content-Transfer-Encoding: base64",
    "",
    body,
    "",
    "--" + boundary,
  ].join("\r\n");
  return Utilities.base64EncodeWebSafe(mailData);
}

function myFunction() {

  // Please declare email and firstName.

  var title = rowData.publicationTitle;
  var journal = rowData.journalTitle;
  var url = rowData.publicationUrl;
  //Emoji goes here in the body:
  var body = "Hi " + firstName + "!<br><br>I noticed your article <a href='" + url + "'>&ldquo;" + title + "&rdquo;</a> was recently published in <i>" + journal + "</i>. Congratulations! This is just a friendly reminder to please upload your original document and a PDF version to our publications app when you have time.<br><br>To upload your publication, you can <a href='http://support.cpes.vt.edu/publishing'>click here</a>.<br><br>Thanks!<br><br> CB<br><br><hr style='background-color: #d8d8d8; border: 0 none; color: #d8d8d8; height: 1px;'><span style='font-size:12px'><b>CPES Publications Reminders</b>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='mailto:leshutt@vt.edu' style='text-decoration:none'>Feedback</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='http://support.cpes.vt.edu/publishing' style='text-decoration:none;'>Publication uploads</a></span>";
  var emailSubject = "Just a reminder to upload your article!";
  var me = Session.getActiveUser().getEmail();
  var aliases = GmailApp.getAliases();
  if (emailStatus == "Pending" && emailData !== "No emails found"){
    // Added script
    var raw = convert(email, aliases[2], emailSubject, body);
    Gmail.Users.Messages.send({raw: raw}, "me");
  }
}
笔记 :
  • 当您使用此样品时,请声明emailfirstName
  • 请运行myFunction()

参考 :

如果我误解了你的问题,我很抱歉。


推荐阅读