Read Gmail Messages with Apps Script

GmailApp lets you search your inbox, read message content, and extract metadata like sender, subject, and body. This is useful for processing inbound requests, building email-to-sheet pipelines, or automating follow-ups based on customer replies.

Searching for Messages

Use GmailApp.search(query) with standard Gmail search syntax:

function findCustomerEmails() { var threads = GmailApp.search('from:[email protected] subject:"Pro Plan"'); threads.forEach(function(thread) { var messages = thread.getMessages(); messages.forEach(function(msg) { Logger.log("From: " + msg.getFrom()); Logger.log("Subject: " + msg.getSubject()); Logger.log("Date: " + msg.getDate()); Logger.log("Body: " + msg.getPlainBody().substring(0, 200)); }); }); }

Reading Unread Messages from a Specific Sender

function readUnreadFromCustomers() { var threads = GmailApp.search('is:unread from:(@techventures.com OR @deltatech.io OR @innovateco.com)'); threads.forEach(function(thread) { var msg = thread.getMessages()[0]; // First message in thread Logger.log("From: " + msg.getFrom()); Logger.log("Subject: " + msg.getSubject()); thread.markRead(); // Mark as read after processing }); }

Writing Email Data to a Sheet

This example searches for deal-related emails and logs them into a Google Sheet for tracking:

function logCustomerRepliesToSheet() { var sheet = SpreadsheetApp .getActiveSpreadsheet() .getSheetByName("Email Log") || SpreadsheetApp.getActiveSpreadsheet().insertSheet("Email Log"); sheet.clearContents(); sheet.appendRow(["From", "Subject", "Date", "Snippet"]); var threads = GmailApp.search('subject:"Acme Corp" is:inbox newer_than:7d'); threads.forEach(function(thread) { thread.getMessages().forEach(function(msg) { sheet.appendRow([ msg.getFrom(), msg.getSubject(), msg.getDate(), msg.getPlainBody().substring(0, 150) ]); }); }); Logger.log("Email log written to sheet."); }

Checking for Customer Replies from Sheet Data

This example reads customer emails from the Sales Tracker and checks if any have replied in the last 7 days:

function checkForCustomerReplies() { var sheet = SpreadsheetApp .getActiveSpreadsheet() .getSheetByName("Sales Tracker"); var data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 8).getValues(); // Columns: [Customer Name, Email, Product, Amount, Region, Sales Rep, Status, Date] data.forEach(function(row) { var customerName = row[0]; var customerEmail = row[1]; var query = 'from:' + customerEmail + ' newer_than:7d'; var threads = GmailApp.search(query); if (threads.length > 0) { Logger.log(customerName + " has replied recently."); } else { Logger.log(customerName + " has not replied in 7 days."); } }); }

Getting Attachments from a Message

function downloadAttachments() { var threads = GmailApp.search('subject:"Signed Contract" has:attachment'); threads.forEach(function(thread) { thread.getMessages().forEach(function(msg) { msg.getAttachments().forEach(function(attachment) { var folder = DriveApp.getFolderById("YOUR_CONTRACTS_FOLDER_ID"); folder.createFile(attachment); Logger.log("Saved: " + attachment.getName()); }); }); }); }

This pattern is useful for auto-saving signed contracts or invoices sent by customers directly into Drive.