Auto-Reply Gmail Emails Using Google Apps Script (JavaScript)
Automating email responses is a game-changer for productivity. Whether you’re managing customer support, internal communication, or just want cleaner inbox management, Google Apps Script (GAS) offers a powerful and accessible way to build smart auto-replies directly into your Gmail account using JavaScript.
In this blog post, we’ll walk through building an auto-reply system in Gmail using Google Apps Script. We’ll detect specific keywords in incoming emails and send contextual replies automatically. You’ll learn how to set up triggers, filter messages, send dynamic replies, and manage performance efficiently.
1. Understanding Google Apps Script and Gmail Integration
Google Apps Script is a JavaScript-based language built into Google Workspace. It allows you to automate tasks across Gmail, Sheets, Drive, etc. For this project, we’ll use the Gmail service in GAS to access messages, analyze their content, and reply where relevant.
Start by opening https://script.google.com and creating a new project:
// File: Code.gs
function autoReplyToEmails() {
var threads = GmailApp.getInboxThreads(0, 20); // Check last 20 threads
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var lastMsg = messages[messages.length - 1];
if (!lastMsg.isReplied()) {
var body = lastMsg.getPlainBody().toLowerCase();
if (body.includes("pricing") || body.includes("quote")) {
lastMsg.reply("Hi there! Here is our pricing guide: [link]. Let us know if you need anything else.");
}
}
}
}
This function checks the latest threads, looks for keywords, and replies when appropriate.
2. Setting Up Time-Driven Triggers
We don’t want to run this manually every time. We'll set up a time-driven trigger so the script runs periodically (e.g., every 10 minutes).
// Run this once to set the trigger
function createTimeDrivenTrigger() {
ScriptApp.newTrigger("autoReplyToEmails")
.timeBased()
.everyMinutes(10)
.create();
}
Save the script and run createTimeDrivenTrigger() once from the Apps Script editor. This will automatically invoke autoReplyToEmails() every 10 minutes.
3. Filtering Emails with More Context and Accuracy
To minimize false positives, let’s only target messages from external senders and older than 2 minutes (to avoid race conditions).
function autoReplyToEmails() {
var threads = GmailApp.search("is:inbox -from:me newer_than:1d");
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var lastMsg = messages[messages.length - 1];
if (!lastMsg.isReplied()) {
var from = lastMsg.getFrom();
if (!from.includes("@yourdomain.com")) {
var body = lastMsg.getPlainBody().toLowerCase();
if (body.includes("support") || body.includes("help")) {
lastMsg.reply("Thanks for reaching out! Our support team will respond shortly.");
} else if (body.includes("pricing") || body.includes("quote")) {
lastMsg.reply("You can view our current pricing here: https://example.com/pricing.");
}
}
}
}
}
We added Gmail Search operators and improved the filter logic. This update ensures better targeting and avoids replying to your own messages.
4. Customizing Responses Dynamically
Let’s personalize our replies using name extraction from the email’s “From” field. This improves engagement and feels more human.
function extractName(fromField) {
var match = fromField.match(/^(.*?)\s*);
return match ? match[1] : "there";
}
function autoReplyToEmails() {
var threads = GmailApp.search("is:inbox -from:me newer_than:1d");
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var lastMsg = messages[messages.length - 1];
if (!lastMsg.isReplied()) {
var body = lastMsg.getPlainBody().toLowerCase();
var from = lastMsg.getFrom();
var name = extractName(from);
var reply = null;
if (body.includes("pricing")) {
reply = `Hi ${name},\n\nThanks for your interest! Here's where you can view pricing: https://example.com/pricing.`;
} else if (body.includes("help")) {
reply = `Hi ${name},\n\nThank you for contacting support. We’ll be in touch shortly.`;
}
if (reply) {
lastMsg.reply(reply);
}
}
}
}
This simple name extraction gives us more personalized replies with minimal additional complexity.
5. Performance, Quota, and Best Practices
Google Apps Script has quotas you must respect, including Gmail read limits and reply quotas. Here’s how to improve performance:
- Limit processed threads (
getInboxThreads(0, 10)) to avoid excess processing. - Avoid replying to emails more than once by tagging them or checking flags like
isReplied(). - Use Gmail thread labels (e.g.,
"Auto Replied") for better tracking.
Here's how you could label threads after replying:
function getOrCreateLabel(name) {
var label = GmailApp.getUserLabelByName(name);
return label || GmailApp.createLabel(name);
}
function autoReplyToEmails() {
var label = getOrCreateLabel("Auto Replied");
var threads = GmailApp.search("is:inbox -from:me -label:auto-replied newer_than:1d");
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var lastMsg = messages[messages.length - 1];
var body = lastMsg.getPlainBody().toLowerCase();
var from = lastMsg.getFrom();
var name = extractName(from);
var reply = null;
if (body.includes("demo")) {
reply = `Hi ${name},\n\nYou can schedule a demo with us here: https://example.com/demo.`;
}
if (reply) {
lastMsg.reply(reply);
threads[i].addLabel(label);
}
}
}
Conclusion
Using Google Apps Script, you can easily build a smart Gmail auto-responder tailored to your workflow. With keyword filters, contextual replies, dynamic personalization, and timed triggers, this solution boosts your efficiency while maintaining professionalism.
This demo lays the foundation. You can refine it further by integrating Google Sheets for configuration, using Gmail advanced search queries, or even connecting to APIs for smarter NLP-based responses.
Happy automating!
Useful links:


