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

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

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

We will never send any spam emails. Promise.