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(
'[email protected]', 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?
I believe your goal as follows.
îăť
in the subject using Google Apps Script.GmailApp.sendEmail
without using MailApp.sendEmail
.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.îăť
in the email subject, the subject is sent as the base64 data.
GmailApp.sendEmail
.When above points are reflected to the script, it becomes as follows.
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");
}
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"
);