1
votes

I am trying to send out an email using Google Apps Script.

// try 1
const subject = 'Hello World ????';

// try 2
const subject = 'Hello World ' + String.fromCodePoint('0x1F600');

GmailApp.sendEmail(
  'abc@gmail.com', subject, '',
  {htmlBody: '<p>Hello World ????</p>', name: 'ABC'}
);

When I use a ⭐, it works perfectly in both subject and HTML body. However, when I use ????, it shows black diamonds with question marks in both subject and HTML body.

I have also checked How to insert an emoji into an email sent with GmailApp? but it only showcases how to use it in the body of the email, not the subject.

I have tried using MailApp and it worked but I don't want to use it for some reasons.

Any idea on how to solve this?

1
From your updated question, I understood that you wanted to know the method for achieving your goal using GmailApp.sendEmail without using MailApp.sendEmail. By this, I reopened your question.Tanaike
Hi @Tanaike, any idea on how to achieve the goal?Ravgeet Dhillon
Thank you for replying. About The emoji in the body displays successfully., can I ask you about the method for achieving it? Because when I tested your script, the emoji in the email body cannot be correctly shown. I apologize for this.Tanaike
For example, when the HTML body doesn't include the emoji and you want to include the emoji in only the subject using GmailApp.sendEmail, I think that this can be achieved. How about this?Tanaike

1 Answers

4
votes

I believe your goal as follows.

  • You want to send an gmail including the emoji like ・ in the subject using Google Apps Script.
  • You wanted to know the method for achieving your goal using GmailApp.sendEmail without using MailApp.sendEmail.
  • But, when GmailApp.sendEmail is used, the subject can include the emoji. But the emoji cannot be used in the email body. So I proposed to use Gmail API in this situation. For my question of as other direction, in your goal, can you use Gmail API?, you answered Yes, Gmail API will work.. So I understood that your goal can be achieved using Gmail API.

Modification points:

  • In this case, Gmail API is used with Advanced Google services.
  • And, when you want to include the emoji like ・ in the email subject, the subject is sent as the base64 data.
    • When the value include the emoji is converted to the base64 data, in my test, it seems that the value is required to be converted to the base64 as UFT-8.
    • I confirmed that this workaround can be also used for GmailApp.sendEmail.

When above points are reflected to the script, it becomes as follows.

Sample script:

Before you use this script, please enable Gmail API at Advanced Google services. And, please set the variables in the function main() and run the function main().

function convert(toEmail, fromEmail, name, subject, textBody, htmlBody) {
  const boundary = "boundaryboundary";
  const mailData = [
    `MIME-Version: 1.0`,
    `To: ${toEmail}`,
    `From: "${name}" <${fromEmail}>`,
    `Subject: =?UTF-8?B?${Utilities.base64Encode(subject, Utilities.Charset.UTF_8)}?=`,
    `Content-Type: multipart/alternative; boundary=${boundary}`,
    ``,
    `--${boundary}`,
    `Content-Type: text/plain; charset=UTF-8`,
    ``,
    textBody,
    ``,
    `--${boundary}`,
    `Content-Type: text/html; charset=UTF-8`,
    `Content-Transfer-Encoding: base64`,
    ``,
    Utilities.base64Encode(htmlBody, Utilities.Charset.UTF_8),
    ``,
    `--${boundary}--`,
  ].join("\r\n");
  return Utilities.base64EncodeWebSafe(mailData);
}

// Please run this function.
function main() {
  const toEmail = "###"; // Please set the email for `to`.
  const fromEmail = "###"; // Please set the email for `from`.
  const name = "ABC";
  const subject = "Hello World ・";
  const textBody = "sample text body ・";
  const htmlBody = "<p>Hello World ・</p>";
  var raw = convert(toEmail, fromEmail, name, subject, textBody, htmlBody);
  Gmail.Users.Messages.send({raw: raw}, "me");
}

Note:

  • When you want to use the emoji with the subject using GmailApp.sendEmail, you can also use the following script. But, in this case, in my environment, when the emoji is included in the text body and HTML body, the emoji cannot be seen. So please be careful this.

      const emailAddress = = "###"; // Please set the email for `to`.
      const subject = 'Hello World ・';
      GmailApp.sendEmail(
        emailAddress,
        `=?UTF-8?B?${Utilities.base64Encode(Utilities.newBlob(subject).getBytes())}?=`,
        "sample text body"
      );
    

References: