I thought of a way to not use the canned response as such (including the added response
quote in the email address), but to simply forward the canned response to the filtered emails (recipients). This is how it looks like:
Code
function doGet(e) {
// retrieve Id's from canned response
var drafts = GmailApp.getDraftMessages();
for(var i in drafts) {
Logger.log(drafts[i].getId() + " " + drafts[i].getSubject());
}
// set canned response mail
var canned = GmailApp.getMessageById('1410f11ab42ca12d');
// get all messages
var eMails = GmailApp.getMessagesForThreads(
GmailApp.search("label:cannedresponse label:unread"))
.reduce(function(a, b) {return a.concat(b);})
.map(function(eMails) {
return eMails.getFrom()
});
// sort and filter for unique entries
var aEmails = eMails.sort().filter(function(el,j,a)
{if(j==a.indexOf(el))return 1;return 0});
// forward canned response
for(var j in aEmails) {
canned.forward(aEmails[j]);
}
// mark all as read
var threads = GmailApp.search("label:cannedresponse label:unread");
GmailApp.markThreadsRead(threads);
}
Explained
The first lines are to determine the Id of the canned response you want to send out. The Logger
function will help to log all draf Id's. Add this one particular Id into the getMessageById(Id)
. The next three snippets will get all unread mails from the cannedResponse
label and remove duplicates.
Based on the emails present, the canned responses are forwarded to the different recipients. After that, all unread emails in the cannedResponse label are marked as read.
Usage
Add the code by surfing to the following URL: https://script.google.com.
Make sure to press the "bug" button to authenticate the script and its usage. The script can be triggered on a time based interval. Set the trigger in the Resources
menu, the way you like.
In this case, there are some prerequisites. You need to have a label called cannedResponse
and to have a canned response as draft. Furthermore, a filter needs to be in place, in order to send out canned responses.
Screenshot