Integration

Google Sheets email finder: =FINDEMAIL() and =VERIFYEMAIL()

Find and verify work emails inside a sheet with =FINDEMAIL() and =VERIFYEMAIL().

The functions

FormulaReturns
=FINDEMAIL(first_name, last_name, domain)The work email, only when the mailbox is verified. Blank otherwise.
=FINDEMAIL(first_name, last_name, domain, TRUE)Also returns catch-all and predicted addresses, which are not confirmed.
=FINDEMAILSTATUS(first_name, last_name, domain)verified, catch_all, predicted or not_found.
=VERIFYEMAIL(email)valid, invalid, catch_all or unknown, for an address you already have.

There is also a FindLeadEmails menu. Select three columns (first name, last name, domain) and choose Find emails for selected rows: the email and its status are written into the two columns to the right as plain values. For a list of more than a few dozen rows, use the menu and not the formula.

Install it in two minutes

  1. Create an API key. Sign in to FindLeadEmails, open the Developers page, and create a key. Copy it; it is shown once.
  2. Open Apps Script. In your Google Sheet, choose Extensions, then Apps Script. A code editor opens in a new tab.
  3. Paste the script. Delete whatever is in Code.gs, paste the script from this page, and click Save.
  4. Reload the sheet. Go back to the spreadsheet and reload the page. A FindLeadEmails menu appears next to Help.
  5. Set the key. Choose FindLeadEmails, then Set API key, and paste your key. Google asks you to authorize the script the first time.
  6. Use it. Type =FINDEMAIL(A2, B2, C2) in a cell, where the three cells hold a first name, a last name and a company domain.
Code.gs
Download
/**
 * FindLeadEmails for Google Sheets
 * https://www.findleademails.com/integrations/google-sheets
 *
 * Adds two spreadsheet functions and a menu:
 *   =FINDEMAIL(first_name, last_name, domain)   a verified work email, or blank
 *   =VERIFYEMAIL(email)                          valid, invalid, catch_all or unknown
 *   FindLeadEmails > Find emails for selected rows
 *
 * Billing is the same as the app and the API: one credit when a mailbox is
 * confirmed. Catch-alls, misses and repeat lookups are free.
 */

var API_BASE = 'https://www.findleademails.com/api/v1';
var KEY_PROPERTY = 'FINDLEADEMAILS_API_KEY';
var CACHE_SECONDS = 21600; // 6 hours, the longest CacheService allows
var MAX_ROWS_PER_RUN = 200; // keeps one menu run inside Apps Script's time limit

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('FindLeadEmails')
    .addItem('Set API key…', 'setApiKey')
    .addItem('Find emails for selected rows', 'findSelectedRows')
    .addItem('Verify selected emails', 'verifySelectedEmails')
    .addSeparator()
    .addItem('Remove API key', 'removeApiKey')
    .addToUi();
}

function onInstall(e) {
  onOpen(e);
}

/**
 * The key is stored in this spreadsheet's document properties, because
 * spreadsheet functions cannot read per-user storage reliably. Anyone who can
 * edit this spreadsheet can therefore spend the key's credits.
 */
function setApiKey() {
  var ui = SpreadsheetApp.getUi();
  var response = ui.prompt(
    'FindLeadEmails API key',
    'Create a key on the Developers page of your FindLeadEmails account and paste it here. ' +
      'It is stored with this spreadsheet, so anyone who can edit the spreadsheet can use it.',
    ui.ButtonSet.OK_CANCEL
  );
  if (response.getSelectedButton() !== ui.Button.OK) return;
  var key = response.getResponseText().trim();
  if (!key) return;
  PropertiesService.getDocumentProperties().setProperty(KEY_PROPERTY, key);
  ui.alert('Saved. Try =FINDEMAIL("Jane", "Doe", "company.com") in a cell.');
}

function removeApiKey() {
  PropertiesService.getDocumentProperties().deleteProperty(KEY_PROPERTY);
  SpreadsheetApp.getUi().alert('The API key was removed from this spreadsheet.');
}

function getApiKey_() {
  var key = PropertiesService.getDocumentProperties().getProperty(KEY_PROPERTY);
  if (!key) throw new Error('Set your API key first: FindLeadEmails > Set API key…');
  return key;
}

/** POSTs JSON, waiting out rate limits. Returns { status, body }. */
function post_(path, payload) {
  var options = {
    method: 'post',
    contentType: 'application/json',
    headers: { 'x-api-key': getApiKey_() },
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };
  for (var attempt = 0; attempt < 4; attempt++) {
    var response = UrlFetchApp.fetch(API_BASE + path, options);
    var status = response.getResponseCode();
    var body = {};
    try {
      body = JSON.parse(response.getContentText());
    } catch (error) {
      body = {};
    }
    if (status !== 429) return { status: status, body: body };
    var wait = Number(body.retry_after_seconds) || 1;
    Utilities.sleep(Math.min(wait, 10) * 1000 + 250);
  }
  return { status: 429, body: { error: 'Rate limited. Try again in a minute.' } };
}

function explain_(result) {
  if (result.status === 401) return 'The API key was not accepted. Set it again: FindLeadEmails > Set API key…';
  if (result.status === 402) return 'Out of credits. Add credits at https://www.findleademails.com/pricing';
  if (result.status === 429) return 'Rate limited. Try again in a minute.';
  return (result.body && result.body.error) || 'Lookup failed (HTTP ' + result.status + ').';
}

function clean_(value) {
  return String(value === null || value === undefined ? '' : value).trim();
}

function lookup_(firstName, lastName, domain) {
  var first = clean_(firstName);
  var last = clean_(lastName);
  var company = clean_(domain);
  if (!first || !last || !company) return { email: '', status: 'missing_input' };

  var cache = CacheService.getDocumentCache();
  var cacheKey = 'find:' + [first, last, company].join('|').toLowerCase();
  var cached = cache.get(cacheKey);
  if (cached) return JSON.parse(cached);

  var result = post_('/email/find', { first_name: first, last_name: last, company_domain: company });
  if (result.status !== 200) throw new Error(explain_(result));
  var value = { email: result.body.email || '', status: result.body.status || 'not_found' };
  cache.put(cacheKey, JSON.stringify(value), CACHE_SECONDS);
  return value;
}

/**
 * Finds a work email and confirms the mailbox with the company's mail server.
 * Returns the address only when it is verified, unless includeUnverified is TRUE.
 *
 * @param {string} firstName The person's first name.
 * @param {string} lastName The person's last name.
 * @param {string} domain The company's domain, such as company.com.
 * @param {boolean} includeUnverified Optional. TRUE also returns catch-all and predicted addresses.
 * @return The email address, or blank when none was confirmed.
 * @customfunction
 */
function FINDEMAIL(firstName, lastName, domain, includeUnverified) {
  var result = lookup_(firstName, lastName, domain);
  if (result.status === 'verified') return result.email;
  return includeUnverified === true ? result.email : '';
}

/**
 * The status of a FINDEMAIL lookup: verified, catch_all, predicted or not_found.
 *
 * @param {string} firstName The person's first name.
 * @param {string} lastName The person's last name.
 * @param {string} domain The company's domain, such as company.com.
 * @return The lookup status.
 * @customfunction
 */
function FINDEMAILSTATUS(firstName, lastName, domain) {
  return lookup_(firstName, lastName, domain).status;
}

function verify_(email) {
  var address = clean_(email);
  if (!address) return 'missing_input';
  var cache = CacheService.getDocumentCache();
  var cacheKey = 'verify:' + address.toLowerCase();
  var cached = cache.get(cacheKey);
  if (cached) return cached;
  var result = post_('/verify', { email: address });
  if (result.status === 400) return 'invalid';
  if (result.status !== 200) throw new Error(explain_(result));
  var status = result.body.status || 'unknown';
  cache.put(cacheKey, status, CACHE_SECONDS);
  return status;
}

/**
 * Checks whether a mailbox exists without sending a message.
 *
 * @param {string} email The address to check.
 * @return valid, invalid, catch_all or unknown.
 * @customfunction
 */
function VERIFYEMAIL(email) {
  return verify_(email);
}

/**
 * Menu action. Select three columns (first name, last name, domain). The
 * email and its status are written into the two columns to the right, as
 * plain values that do not recalculate.
 */
function findSelectedRows() {
  var ui = SpreadsheetApp.getUi();
  var range = SpreadsheetApp.getActiveRange();
  if (!range || range.getNumColumns() !== 3) {
    ui.alert('Select three columns: first name, last name and company domain. Results go in the two columns to the right.');
    return;
  }
  var rows = range.getValues();
  var count = Math.min(rows.length, MAX_ROWS_PER_RUN);
  var output = [];
  var verified = 0;
  for (var i = 0; i < count; i++) {
    try {
      var result = lookup_(rows[i][0], rows[i][1], rows[i][2]);
      if (result.status === 'verified') verified++;
      output.push([result.email, result.status === 'missing_input' ? '' : result.status]);
    } catch (error) {
      output.push(['', String(error.message || error)]);
      if (/API key|credits/i.test(String(error.message))) break;
    }
  }
  if (output.length) range.offset(0, 3, output.length, 2).setValues(output);
  var note = output.length < rows.length
    ? ' Stopped after ' + output.length + ' of ' + rows.length + ' rows. Select the rest and run it again.'
    : '';
  SpreadsheetApp.getActive().toast(verified + ' verified of ' + output.length + ' rows.' + note, 'FindLeadEmails', 10);
}

/** Menu action. Select one column of addresses. The status is written into the column to the right. */
function verifySelectedEmails() {
  var ui = SpreadsheetApp.getUi();
  var range = SpreadsheetApp.getActiveRange();
  if (!range || range.getNumColumns() !== 1) {
    ui.alert('Select one column of email addresses. The status goes in the column to the right.');
    return;
  }
  var rows = range.getValues();
  var count = Math.min(rows.length, MAX_ROWS_PER_RUN);
  var output = [];
  for (var i = 0; i < count; i++) {
    try {
      var status = verify_(rows[i][0]);
      output.push([status === 'missing_input' ? '' : status]);
    } catch (error) {
      output.push([String(error.message || error)]);
      if (/API key|credits/i.test(String(error.message))) break;
    }
  }
  if (output.length) range.offset(0, 1, output.length, 1).setValues(output);
  var note = output.length < rows.length
    ? ' Stopped after ' + output.length + ' of ' + rows.length + ' rows. Select the rest and run it again.'
    : '';
  SpreadsheetApp.getActive().toast('Checked ' + output.length + ' addresses.' + note, 'FindLeadEmails', 10);
}

No account yet? Create one and you get welcome credits to try it with. The API itself is documented at /docs.

What to know before you use it

  • The key lives with the spreadsheet. Spreadsheet functions cannot use per-user storage, so the key is saved in the document. Anyone who can edit the spreadsheet can spend its credits. Use a key made for this sheet, and remove it from the menu when you are done.
  • Use the company domain, not the company name. company.com, not “Company Inc”.
  • A blank is an answer. FINDEMAIL returns nothing when no mailbox could be confirmed. Use FINDEMAILSTATUS to see whether that was a miss or a catch-all domain, where no tool can confirm a specific person.
  • Google gives a formula 30 seconds. A slow mail server can take longer. The menu action does not have that limit, and processes 200 rows per run.
  • For thousands of rows, upload the file. The bulk finder takes a CSV and is built for that volume.

Frequently asked questions

How do I find email addresses in Google Sheets?
Add the FindLeadEmails script to the sheet through Extensions, Apps Script, set your API key from the FindLeadEmails menu, and use =FINDEMAIL(first name, last name, domain). It returns the work email once the company's mail server has confirmed the mailbox.
What does it cost to use FINDEMAIL in a sheet?
The same as the app and the API: one credit when a mailbox is verified. Catch-alls, misses and repeat lookups of the same person are free. A new account gets welcome credits, and credits never expire.
Will the formulas spend credits every time the sheet recalculates?
No. The script caches each result for six hours, and a repeat lookup of a person already found is free on our side as well. For a long list, use the menu action, which writes plain values that never recalculate.
Is this an official Google Workspace Marketplace add-on?
Not yet. Today it is a script you paste into your own sheet, which takes about two minutes and needs no approval. The code is on this page in full, so you can read exactly what it does.

Connect Google Sheets

Find and verify work emails inside a sheet with =FINDEMAIL() and =VERIFYEMAIL().

Get started

Related