如何在使用 GmailApp 发送的电子邮件中插入表情符号?

Lau*_*ren 9 email gmail google-apps-script emoji

我有一个发送自动电子邮件的 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
  });
}
Run Code Online (Sandbox Code Playgroud)

我注意到发送星号 ("?") 有效,但普通的笑脸 ("") 显示为黑白、Unicode 风格的图标,我尝试过的其他所有内容都是问号。您只能使用特定 Unicode 版本的表情符号吗?

Tan*_*ike 10

您想发送包含表情符号的 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, {
Run Code Online (Sandbox Code Playgroud) 到 :
MailApp.sendEmail(email, emailSubject, body, {
Run Code Online (Sandbox Code Playgroud)

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");
  }
}
Run Code Online (Sandbox Code Playgroud) 笔记 :
  • 当您使用此样本时,请声明emailfirstName
  • 请跑myFunction()

参考 :

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


Mic*_*ani 5

我尝试复制表情符号(https://www.emojicopy.com/)并将其直接粘贴到脚本编辑器中:

在此处输入图片说明

发送电子邮件后,我在邮箱中收到了它:

在此处输入图片说明

编辑:

请注意,某些表情符号长度为 1 个字符(例如星星),但其他表情符号长度为 2 个字符(例如微笑),对于那些有 2 个字符的表情符号,您可以考虑在微笑后立即书写,但您是在微笑内书写,因此您将其打破因此变成问号。

如果您尝试运行此代码,您将看到第一个长度为 2,第二个长度为 1:

在此处输入图片说明

如果您尝试在这 2 个表情符号上移动指针(在应用程序脚本编辑器中),从表情符号之前到之后,您将看到在星星的情况下只需一步,但对于微笑,您需要 2 步。