This is my attempt, however, I'm unable to get the draft to update -- I want the text "This Message has been verified" to be added while retaining the draft contents, recipients, and subject.
function dlpValidate(e) {
var toEmails = e.draftMetadata.toRecipients, ccEmails = e.draftMetadata.ccRecipients, bccEmails = e.draftMetadata.bccRecipients, domains = [], uniqueDomains = [];
var allEmails = toEmails.concat(ccEmails, bccEmails);
for (var i = 0; i < allEmails.length; i++) {
domains[i] = allEmails[i].split("@").pop().split(".")[0];
}
uniqueDomains = domains.filter(listUnique);
if(uniqueDomains.length == 1 || (uniqueDomains.length <= 2 && uniqueDomains.indexOf("verasafe") != -1)) {
var draft = GmailApp.getDrafts()[0];
draft.update(toEmails,"test","This Message has been verified");
return [notifyYes(uniqueDomains)];
}
}
The @param {event} e is the compose trigger event object, within a Compose trigger function that fires when the compose action is selected: Reference.
This is the function listUnique
, called in the main function:
function listUnique(value, index, self) { return self.indexOf(value) === index; }
As it's currently written, the function replaces the draft contents entirely, instead of appending it. Additionally, it removes the recipients under CC and/or BCC fields and places them in the TO field.
GmailDraft.update
replaces all current contents of the draft, as specified in the official documentation. You have to set everything again (subject, content, recipient, cc, bcc, etc.).
So if you just want to append the message "This Message has been verified" to the content and you want to keep everything else as it is, you should first retrieve the data about the current draft, then append whatever you want to this data, and finally update the draft with the appended data.
You could do something along the following lines:
function dlpValidate(e) {
var toEmails = e.draftMetadata.toRecipients,
ccEmails = e.draftMetadata.ccRecipients,
bccEmails = e.draftMetadata.bccRecipients,
domains = [],
uniqueDomains = [];
var allEmails = toEmails.concat(ccEmails, bccEmails);
for (var i = 0; i < allEmails.length; i++) {
domains[i] = allEmails[i].split("@").pop().split(".")[0];
}
uniqueDomains = domains.filter(listUnique);
if(uniqueDomains.length == 1 || (uniqueDomains.length <= 2 && uniqueDomains.indexOf("verasafe") != -1)) {
var draft = GmailApp.getDrafts()[0];
var message = draft.getMessage();
var subject = message.getSubject();
var content = message.getBody();
// Append "This Message has been verified" to the current draft body
content = content.concat("<div>This Message has been verified</div>");
var options = { // Set cc, bcc and body for the updated draft
bcc: bccEmails.join(","),
cc: ccEmails.join(","),
htmlBody: content
}
draft.update(toEmails, subject, content, options); // Update draft
return [notifyYes(uniqueDomains)];
}
}
I hope this is of any help.