How To Use Emoji In Email Subject Using Google Apps Script?
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');
Solution 1:
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 usingMailApp.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 ofas other direction, in your goal, can you use Gmail API?
, you answeredYes, 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()
.
functionconvert(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");
returnUtilities.base64EncodeWebSafe(mailData);
}
// Please run this function.functionmain() {
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" );
Post a Comment for "How To Use Emoji In Email Subject Using Google Apps Script?"