How to extract email addresses from Gmail

How to extract email addresses from Gmail

Over the years, extracting email addresses has become an integral process of lead generation. Businesses believe they cannot grow as exponentially as they should without lead generation. According to statistical data sourced by Forbes, over 58 percent of marketers believe that generating leads is key to overcoming growth-relevant business challenges. 

Finding as well as validating the email addresses of prospective clients from Gmail can turn out to be a time-consuming task. So, to overcome this challenge, we have come up with the ultimate Google App Script code to extract the email addresses of your prospective clients from Gmail. Most email marketers and businesses can use this Google Apps Script Code to overcome this challenge. 

Very few email extraction tools would serve as an alternative in processing essential lead data as seamlessly as the below-mentioned Google Apps Script Code. While most email extractors out there offer many features such as data enrichment, CRM integrations, bulk export, and import, sending personalized emails, email verification, etc., there is a Google Apps Script Code for everything. 

Here’s a detailed guide on how to extract email addresses from Gmail using the Google Apps Script Code in the most hassle-free way:

Google has drastically changed the world wide web unlike anyone else. Perhaps, this is one of the biggest reasons why, email clients across the globe, trust Gmail the most when it comes to using email services. Presently, there are over 1.5 billion Gmail users worldwide. 

With Gmail’s ease of integration with eminent 3rd party applications, powerful flexibility, as well as powerful analytical features, Gmail has surely become a fan favorite. Over 44 percent of users across the globe prefer using Gmail the most frequently as compared to other popular email services providers like Yahoo Mail, Hotmail, and Outlook among others. 

Gmail maintains 3 email lists for aiding businesses to stay in touch with their associates as well as clients. The list “My Contacts” consists of all those email contacts that one saves in Gmail. If one has had contact with some email addresses previously, then Gmail can even track those email addresses. Apart from creating a subset of these contacts, Gmail will also track the prevalence of these contacts. What more? One can even create groups for further organization of contacts. These email lists can easily be extracted using the export feature of Gmail. 

In this article, you will be learning all about the important steps you must follow to extract email addresses from Gmail using the Google Apps Script code. 

You can use the below Apps Script code to extract email addresses from Gmail label.

function extractEmailAddresses() {
  var emailAddresses = [];
  
  // Replace "INBOX" with the label you want to search
  var threads = GmailApp.search("label:INBOX");
  
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var message = messages[j];
      var subject = message.getSubject();
      var to = message.getTo();
      var cc = message.getCc();
      var bcc = message.getBcc();
      
      // Add the email addresses from the "To" field to the emailAddresses array
      emailAddresses = emailAddresses.concat(to);
      
      // Add the email addresses from the "Cc" field to the emailAddresses array
      emailAddresses = emailAddresses.concat(cc);
      
      // Add the email addresses from the "Bcc" field to the emailAddresses array
      emailAddresses = emailAddresses.concat(bcc);
    }
  }
  
  // Remove duplicates from the emailAddresses array
  emailAddresses = emailAddresses.filter(function(item, pos) {
    return emailAddresses.indexOf(item) == pos;
  });
  
  // Log the email addresses
  Logger.log(emailAddresses);
}

This script searches for emails in the “INBOX” label and extracts the email addresses from the “To”, “Cc”, and “Bcc” fields of each email. It then removes any duplicates and logs the email addresses.

To search for emails based on matching words in the subject, you can modify the GmailApp.search() method like this:

var threads = GmailApp.search("label:INBOX subject:searchterm");

Replace “searchterm” with the word or phrase you want to search for.

To search for emails based on a search criteria, you can use the GmailApp.search() method like this:

var threads = GmailApp.search("label:INBOX criteria");

Replace “criteria” with your search criteria. You can use the same search operators that you would use when searching for emails in Gmail. For example, you can use from: to search for emails from a specific sender, subject: to search for emails with a specific subject, has:attachment to search for emails with attachments, and so on.

This is a very useful way to extract emails that can be further used to feed your emailing services like Mailchimp, ActiveCampaign etc. or your CRM like Salesforce.