I want to send a newsletter using MailChimp to everyone I've had an email conversation with over the past year.

What is the best way to export their email addresses?

I only want people I've gone back and forth with (I've sent them at least one email and they've sent me at least one email).

I'm on a Mac and can use Apple Mail if that helps.


Use this script to efficiently retrieve the addresses you want (given the time span) and remove duplicates:


function getEmails() { 
  // http://stackoverflow.com/a/12029701/1536038  
  // get all messages      
  var eMails = GmailApp.getMessagesForThreads(
    GmailApp.search('after:2012/1/14 before:2013/8/15'))
      .reduce(function(a, b) {return a.concat(b);})
      .map(function(eMails) {
    return eMails.getFrom() 

  // sort and filter for unique entries  
  var aEmails = eMails.sort().filter(function(el,j,a)
    {if(j==a.indexOf(el))return 1;return 0});  

  // create 2D-array
  var aUnique = new Array();  
  for(var k in aEmails) {

  // add data to sheet
  SpreadsheetApp.getActiveSheet().getRange(1, 1, aUnique.length, 1)
  • This is definitely better than what I had. However, the end result is missing a significant number of email addresses that I know should be on the list. Perhaps you're hitting an API limit somewhere?
    – Alex
    Aug 14 '13 at 21:52
  • This script will read approx. 3000 items across your mailbox in 5 minutes. Beyond that it will stop. Try adding the extra search parameter in the duplicate post: "label:" + labels[i].getName() + "after:2012/1/14 before:2013/8/15". This way it's confined to the label. Aug 14 '13 at 22:10
  • @Alex forgot to hail you... Aug 14 '13 at 22:10

I think I wrote a script to do this in Google Apps Script

function processInbox() {
  // get all threads within the time range
  var threads =   GmailApp.search('after:2012/1/14 before:2014/8/15');
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // find all threads with more than one message     
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    // get all threads longer than 1
    if (messages.length > 1)
      // get all participants in those threads and add them to a spreadsheet
      for (var j = 0; j< messages.length; j++){
  • 1
    Your script has some major flaws. First, the number of API calls is huge. The script, in my gmail, didn't even complete. Second, your code didn't take into account the duplicates. My script runs in 21 sec., yours didn't complete in the first place !! Aug 14 '13 at 20:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.