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);
}
function main() {
const toEmail = "###";
const fromEmail = "###";
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 = = "###";
const subject = 'Hello World ・';
GmailApp.sendEmail(
emailAddress,
`=?UTF-8?B?${Utilities.base64Encode(Utilities.newBlob(subject).getBytes())}?=`,
"sample text body"
);
References:
GmailApp.sendEmail
without usingMailApp.sendEmail
. By this, I reopened your question. – TanaikeThe 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. – TanaikeGmailApp.sendEmail
, I think that this can be achieved. How about this? – Tanaike