Skip to content

Instantly share code, notes, and snippets.

@erronjason
Created December 15, 2015 18:40
Show Gist options
  • Save erronjason/cac941d5160700628d1b to your computer and use it in GitHub Desktop.
Save erronjason/cac941d5160700628d1b to your computer and use it in GitHub Desktop.
Gmail auto-responder for use in https://script.google.com
function AutoResponder(e) {
// Our email account that will be recieving:
var email_account = "testemail@gmail.com";
// Search for subject:
var subject = "testsubject";
// Send our response email
var threads = GmailApp.search("subject:(" + subject + ") label:unread to:(" + email_account + ")");
for (var i = 0; i < threads.length; i++) {
// This is where further logic would go
var msg = threads[i].getMessages()[0];
var word_count = msg.getBody().split(' ').length;
//Our response
var response_body = "Hey there, this is a test auto-responder.<br>\
Number of words: " + word_count + "<br>\
Original body: " + msg.getBody();
// Respond to email
threads[i].reply("", {htmlBody: response_body, from: email_account});
}
// Mark all as read
var threads = GmailApp.search("subject:(Contact Form) label:unread to:(" + email_account + ")");
GmailApp.markThreadsRead(threads);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment