By Raj

Part of our Google Workspace Automation guides. Need this built for your team? Hire a Google Apps Script developer.

Estimated reading time: 10 minutes

Gmail Automation with Google Apps Script: Filters, Labels & Auto-Reply

GmailApp automates labels, filters, auto-replies, and archiving at volumes the Gmail UI cannot batch efficiently. Scripts run as the mailbox owner who authorized the project.

Use GmailApp.search with queries like label:unprocessed older_than:1d. Process threads in chunks respecting send quotas when replying.

For parsing bodies into Sheets, see /blog/apps-script-gmail-parser. Workspace admins may require domain-wide delegation for service accounts—prefer user-owned scripts.

Labels and inbox rules

Create labels with getUserLabelByName or createLabel. Apply with thread.addLabel after classifying subject lines via regex.

Avoid label proliferation—map to business categories only.

Auto-reply patterns

thread.reply(body, { noReply: true }) for out-of-office style responses only when custom header not present to prevent loops.

Check thread.getMessageCount() and last message from:self before replying.

Route to Sheets or Slack

Extract structured fields, append to Sheet, mark thread processed. Combine with /blog/google-sheets-send-slack-notification for ops visibility.

Never auto-forward PCI or HIPAA content without policy sign-off.

Send and read quotas

Daily send limits apply to MailApp/GmailApp—batch digests. Reading thousands of threads takes time—cursor in Properties.

See /blog/google-apps-script-quotas-and-limits.

Example code

function labelInvoices() {
  const threads = GmailApp.search('subject:(invoice) newer_than:7d -label:processed', 0, 50);
  threads.forEach(function (t) {
    t.addLabel(GmailApp.getUserLabelByName('Invoices') || GmailApp.createLabel('Invoices'));
    const msg = t.getMessages()[t.getMessageCount() - 1];
    SpreadsheetApp.openById('LEDGER_ID').getSheetByName('Inbox').appendRow([new Date(), msg.getSubject(), msg.getFrom()]);
    t.addLabel(GmailApp.getUserLabelByName('processed'));
  });
}
ApproachBest forTradeoff
Apps Script nativeGoogle Workspace-centric workflows6-min limit, quotas
Zapier / MakeNo-code, many connectorsPer-task cost, vendor lock-in
Python + CloudHeavy data / MLHosting cost, separate auth
Google Workspace automationProduction custom logicBuild cost, you own code

FAQ

GmailApp vs MailApp?

GmailApp accesses mailbox threads/labels. MailApp sends email without reading inbox—pick based on need.

Can I run on all domain mailboxes?

Requires admin delegation or each user runs copy—centralized tools often use group mailbox.

Trigger for new mail?

Time-driven search every 5–15 minutes; Gmail push via Cloud Pub/Sub is advanced alternative outside pure Script.

Attachment handling?

message.getAttachments() returns Blobs—save to Drive with DriveApp.createFile.

Avoid infinite auto-reply loops?

Check headers and apply label before sending reply; exclude noreply@ senders.

Need this done for you? I handle this as part of my consulting work — fixed-price quote within 24 hours.

Book a call with Raj →

Get the full Gmail Automation with Google Apps Script script template

I'll email you a production-ready, commented version you can deploy in 10 minutes.

Need help with this? I handle this as part of my Google Workspace Automation service.

Gmail, Drive, Docs, Forms, and Calendar automation.

See how it works →