Google Script for Extracting Email Addresses from Gmail

This Google Apps Script will sift through your Gmail account and extract email addresses of senders which are then saved in a Google Sheet. Useful for email marketing and mail merge.

//   Written by Amit Agarwal on 06/13/2013
function extractEmailAddresses() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var monitor = sheet.getRange('A2').getValue();
  var processed = sheet.getRange('B2').getValue();

  var label = GmailApp.getUserLabelByName(processed);
  var search = 'in:' + monitor + ' -in:' + processed;

  // Process 50 Gmail threads in a batch to prevent script execution errors
  var threads = GmailApp.search(search, 0, 50);

  var row, messages, from, email;

  try {
    for (var x = 0; x < threads.length; x++) {
      // Use Regular Expression to extract valid email address
      from = threads[x].getMessages()[0].getFrom();
      from = from.match(/\S+@\S+\.\S+/g);

      if (from.length) {
        email = from[0];
        email = email.replace('>', '');
        email = email.replace('<', '');

        row = sheet.getLastRow() + 1;
        // If an email address if found, add it to the sheet
        sheet.getRange(row, 1).setValue(email);
      }

      threads[x].addLabel(label);
    }
  } catch (e) {
    Logger.log(e.toString());
    Utilities.sleep(5000);
  }

  // All messages in the label have been processed?
  if (threads.length === 0) {
    GmailApp.sendEmail(Session.getActiveUser().getEmail(), 'Extraction Done', 'Download the sheet from ' + ss.getUrl());
  }
}

// Remove Duplicate Email addresses
function cleanList() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getRange(4, 1, sheet.getLastRow()).getValues();
  var newData = new Array();
  for (i in data) {
    var row = data[i];
    var duplicate = false;
    for (j in newData) {
      if (row[0] == newData[j][0]) {
        duplicate = true;
      }
    }
    if (!duplicate) {
      newData.push(row);
    }
  }

  // Put the unique email addresses in the Google sheet
  sheet.getRange(4, 2, newData.length, newData[0].length).setValues(newData);
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻