Gmail Email Extractor Script
For companies using Google Workspace, maintaining up‑to‑date contact lists and CRM records can be tedious—especially when your sales and support teams send hundreds of emails every day. With a simple Google Apps Script, you can automatically pull every unique recipient address from emails you’ve sent (“from:me”) and dump them into a Google Sheet—no manual copy‑paste required.
The Gmail Email Extractor script (available on GitHub) processes your sent threads in batches, captures unique “To” addresses, and appends them to a sheet. It even resumes where it left off, so you can handle thousands of emails without hitting execution limits.
Why Automate Email Extraction?
- Accelerate Lead Generation
Automatically build prospect lists from outbound campaigns, ensuring your marketing team never misses a contact. - Keep CRM Data Fresh
Sync Gmail with your CRM by exporting all your recent correspondents into Sheets—ready to import into Salesforce, HubSpot, or any platform. - Boost Productivity
Eliminate repetitive copy‑paste work. Your team can focus on strategy instead of data entry. - Scale Securely
Google Apps Script runs on Google’s servers, leveraging your existing Workspace security and compliance controls.

How the Gmail Email Extractor Script Works
function extractEmailAddressesFromMe() {
const userProperties = PropertiesService.getUserProperties();
const lastIndex = parseInt(userProperties.getProperty('LAST_PROCESSED_THREAD_INDEX')||'0',10);
const batchSize = 100;
const threads = GmailApp.search('from:me', lastIndex, batchSize);
let emails = [];
threads.forEach(thread => {
thread.getMessages().forEach(msg => {
msg.getTo().split(',').forEach(addr => {
addr = addr.trim();
if (emails.indexOf(addr) === -1) emails.push(addr);
});
});
});
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if (sheet.getLastRow() === 0) sheet.appendRow(['Email Addresses']);
emails.forEach(address => sheet.appendRow([address]));
if (threads.length === batchSize) {
userProperties.setProperty('LAST_PROCESSED_THREAD_INDEX', lastIndex + batchSize);
ScriptApp.newTrigger('extractEmailAddressesFromMe')
.timeBased().after(1000).create();
} else {
userProperties.deleteProperty('LAST_PROCESSED_THREAD_INDEX');
}
}
- Batch Processing & Resumption
UsesGmailApp.search('from:me', start, size)
to fetch threads in chunks—avoiding script timeouts—then schedules the next batch viaScriptApp.newTrigger(...)
if needed Google for DevelopersGoogle for Developers. - Unique Extraction
Splits each message’s “To” field on commas, trims whitespace, and collects only new addresses to prevent duplicates. - Google Sheet Output
Writes a header row (if empty) and appends each address to the active sheet, creating a living contact directory.
Step‑by‑Step Deployment of Gmail Email Extractor Script
- Create a Google Sheet and open Extensions → Apps Script.
- Paste the Script from the GitHub Gist:
Gmail Email Extractor Gist. - Authorize when prompted—grant access to Gmail and Sheets.
- Run
extractEmailAddressesFromMe()
manually to kick off the first batch. - Monitor Triggers in Apps Script → Triggers; the script will self‑schedule subsequent runs.
For more on Apps Script services:
- GmailApp reference: Google for Developers
- Installable triggers guide: Google for Developers
Real‑World Use Cases of Google Workspace contact collection
- Marketing Teams extract new prospects after email blasts.
- Sales Ops maintain CRM contact directories without manual imports.
- Support Managers audit outreach to track follow‑ups and SLA compliance.
- Data Analysts analyze communication patterns directly in Sheets.
Best Practices & Tips of Google Apps Script automation
- Manage Quotas: Gmail and trigger executions have daily limits. Adjust
batchSize
or addUtilities.sleep()
pauses for very large accounts. - Error Handling: Wrap key operations in
try/catch
and log errors viaLogger.log()
or Stackdriver Logging. - Custom Filtering: Modify the
search()
query (e.g.,"label:Important from:me"
) to target specific threads. - Security: The script runs under your Google account’s OAuth scopes—no credentials are hard‑coded.
Ready to automate your Google Workspace workflow?
Visit vaibhav.co.uk and fill out our Consultation Form to get a custom Apps Script integration for your team.