How can I include email headers information using Google MailApp api?
I need to supply headers info such as List-Unsubscribe:
or List-Unsubscribe-Post:
.
The following is a code sample by Google Apps Script. It seems that there is no option to include such email header info.
sendEmail(recipient, subject, body, options)
// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html',
'my_document.html');
MailApp.sendEmail('mike@example.com', 'Attachment example', 'Two files are attached.', {
name: 'Automatic Emailer Script',
attachments: [file.getAs(MimeType.PDF), blob]
});
I believe your goal as follows.
List-Unsubscribe
and List-Unsubscribe-Post
to the Gmail and send it using Google Apps Script.In this case, how about the following flow?
MailApp.sendEmail
in your script are used.When this flow is reflected to your script, it becomes as follows.
Before you use this script, please enable Gmail API at Advanced Google services.
var obj = {"List-Unsubscribe": "sample1", "List-Unsubscribe-Post": "sample2"}; // Please set the custom headers.
// 1. Create a draft as a temporal.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
var draft = GmailApp.createDraft('mike@example.com', 'Attachment example', 'Two files are attached.', {
name: 'Automatic Emailer Script',
attachments: [file.getAs(MimeType.PDF), blob]
});
// 2. Add the custom headers.
var data = Object.entries(obj).map(([k, v]) => `${k}: ${v}`).join("\n") + "\n" + draft.getMessage().getRawContent();
// 3. Delete Draft.
draft.deleteDraft();
// 4. Send the draft using Gmail API.
var res = Gmail.Users.Messages.send({raw: Utilities.base64EncodeWebSafe(data)}, "me");
console.log(res)
List-Unsubscribe: sample1
and List-Unsubscribe-Post: sample2
are used as the sample values. Please modify this for your actual situation.