How to insert an emoji into an email sent with GmailApp?

痴心易碎 提交于 2021-01-29 06:40:58

问题


I have a GAS script that sends automated emails and would like to include a couple of emojis. I've tried using shortcodes and copying/pasting, but nothing so far seems to have worked. Just wanted to see if there was anything I was missing.

Edit: Here's the code:

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
  });
}

I've noticed that sending a star ("⭐") works, but the normal smiley ("😊") shows up as a black-and-white, Unicode-esque icon and everything else I've tried are question marks. Can you only use emojis up to a certain Unicode release?


回答1:


You want to send an email with HTML body including the emoji. If my understanding is correct, how about this modification?

About GmailApp and MailApp :

  • Unfortunately, GmailApp cannot use the recent emoji characters. At GmailApp

    • emoji less than Unicode 5.2 can be used for this situation.
    • emoji more than Unicode 6.0 can NOT be used for this situation.
  • MailApp can use all versions of emoji.

"⭐" is Unicode 5.1. But "😊" is Unicode 6.0. By this, in your script using GmailApp, you can see the former, but you cannot see the latter. At a sample script of Michele Pisani, the latter is sent using MailApp. So the character is not broken. "🤖" is Unicode 8.0.

Modification points :

So in the case of your script, the modification points are as follows.

  • Use MailApp instead of GmailApp.

OR

  • Use Gmail API.
    • From your comments to Michele Pisani, I worry about that MailApp might not work for your situation. So I would like to also propose the method using Gmail API.

1. Modified your script

Please modify as follows.

From :
GmailApp.sendEmail(email, emailSubject, body, {
To :
MailApp.sendEmail(email, emailSubject, body, {

2. Using Gmail API

In order to use this, please enable Gmail API at Advanced Google Services and API console as follows.

Enable Gmail API v1 at Advanced Google Services

  • On script editor
    • Resources -> Advanced Google Services
    • Turn on Gmail API v1

Enable Gmail API at API console

  • On script editor
    • Resources -> Cloud Platform project
    • View API console
    • At Getting started, click Enable APIs and get credentials like keys.
    • At left side, click Library.
    • At Search for APIs & services, input "Gmail". And click Gmail API.
    • Click Enable button.
    • If API has already been enabled, please don't turn off.

If now you are opening the script editor with the script for using Gmail API, you can enable Gmail API for the project by accessing this URL https://console.cloud.google.com/apis/api/gmail.googleapis.com/overview

Sample script :

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");
  }
}
Note :
  • When you use this sample, Please declare email and firstName.
  • Please run myFunction().

References :

  • Emojipedia
  • Advanced Google Services
  • Gmail API

If I misunderstand your question, I'm sorry.




回答2:


I tried to copy the emoji (https://www.emojicopy.com/) and paste it directly into the script editor:

and after sending the email I received it in my mailbox:

Edit:

Be careful that some emoji are one character length (like the star) but other are 2 characters (like the smile) for those with 2 characters you can think of writing immediately after the smile but instead you are writing inside the smile so you break it therefore turns in question mark.

If you try to run this code, you will see that the first has length 2 and the second one has length 1:

If you try to move the pointer (in the apps script editor) on those 2 emoticons, from before to after the emoticon, you will see that in the case of the star just one step but for the smile you need 2 steps.




回答3:


The easiest complete answer that also works with SMS recipients is this:

function testNewMail() {
 MailApp.sendEmail({
    to: "yourphonenumbergoeshere@mymetropcs.com",
    subject: "Logos",
    htmlBody: "😊 hi todd happy day"
  }); 
}


来源:https://stackoverflow.com/questions/56135360/gmail-shows-emoji-in-the-inbox-but-not-when-email-is-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!