How to insert an emoji into an email sent with GmailApp?
Asked Answered
V

4

13

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:[email protected]' 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?

Vicariate answered 4/6, 2018 at 17:56 Comment(4)
Can you provide the script you used? I think that it will help users think of the solution.Pistole
Had your issue already been solved? If you are still looking for the solution, could you please tell me about your current situation of your issue? If your issue had already been solved, I'm sorry.Pistole
Hi @Pistole I haven't been able to solve it, unfortunately. Whenever I copy and paste emojis into the script, even if I add a bunch of space before and after, it still comes up as question marks in the email. :\Vicariate
Thank you for replying. I posted my answer. Could you please confirm it?Pistole
P
17

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:[email protected]' 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 :

If I misunderstand your question, I'm sorry.

Pistole answered 15/6, 2018 at 23:28 Comment(0)
H
6

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

enter image description here

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

enter image description here

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:

enter image description here

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.

Housley answered 4/6, 2018 at 23:48 Comment(3)
Hi, I tried copying and pasting the same emoji from emojicopy and just got a bunch of question marks like this: ������ I've added my code above, can you take a look if I'm doing something wrong?Vicariate
I modified my answer by adding more detailsHousley
Thanks, I think I need some clarification. I'm trying to use the robot emoji (🤖). I did run the logger code you gave and it came back with "2.0" for the robot face, so I do know it's two characters. I've tried pasting it over multiple spaces, pasting in a space which has extra space on either side, and it still comes back with question marks. Maybe I'm just obtuse - would you mind telling me exactly how to paste it so I don't overwrite the characters? :)Vicariate
S
1

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

function testNewMail() {
 MailApp.sendEmail({
    to: "[email protected]",
    subject: "Logos",
    htmlBody: "😊 hi todd happy day"
  }); 
}
Spire answered 9/12, 2019 at 1:19 Comment(0)
P
0

Use code points (&#128142;) in the value of your cell and send your message as HTML:

var sheet = SpreadsheetApp.getActiveSheet();
var value = sheet.getRange("A1").getValue(); 

GmailApp.sendEmail(
  "[email protected]",
  "subject",
  "",
  {
    from: "[email protected]",
    htmlBody: value
  }
);
Photolithography answered 24/7, 2022 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.