email - 如何在通过GmailApp发送的电子邮件中插入表情符号?

标签 email google-apps-script gmail 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
  });
}

我注意到发送星号(“⭐”)是可行的,但是正常的笑脸(“😊”)显示为黑白的Unicode风格的图标,而我尝试过的其他一切都是问号。您只能使用特定Unicode版本以下的表情符号吗?

最佳答案

您要发送包含表情符号的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

  • Enable Gmail API at API console
  • 关于脚本编辑器
  • 资源->云平台项目
  • View API控制台
  • 在“入门”中,单击“启用API”并获取凭据(例如 key )。
  • 在左侧,单击“库”。
  • 在搜索API和服务时,输入“Gmail”。然后点击Gmail API。
  • 单击启用按钮。
  • 如果已启用API,请不要关闭。

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

    示例脚本:
    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()

  • 引用 :
  • Emojipedia
  • Advanced Google Services
  • Gmail API

  • 如果我误解了您的问题,对不起。

    关于email - 如何在通过GmailApp发送的电子邮件中插入表情符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686254/

    相关文章:

    c# - .NET 2.0 : Sending email to a distribution group

    email - 更改 PHP 邮件函数中的返回路径

    c++套接字-来自电子邮件地址的smtp服务器主机名

    javascript - 如何将变量传递到 HTML 正文中

    html - Gmail iOS 应用程序使用自定义 html 标签 - 有任何信息吗?

    php - 使用 AJAX 在 PHP 中发送电子邮件

    google-apps-script - 如何在本地安装的 IDE 中启用 Google Apps 脚本的自动完成功能

    google-apps-script - 如何更改谷歌应用程序脚本中模态对话的样式

    java - 无法使用java代码从gmail发送邮件

    node.js - 尝试使用 node.js 导入 gmail 联系人