Gmail – Get email addresses from Gmail message body

exportgmailgoogle-apps-scriptgoogle-contacts

I've found this lovely script from another thread:
Get e-mail addresses from Gmail messages received.

It works great! However, what I'm trying to do is pull the contact info from the message body, as a lot of our older emails came from the same info@ourdomain.com address.

All of these emails are incoming (via WordPress contact form 7) and they all have the same message body. The first line in the message is always:

From: Name <name@example.com>

What would I need to do to capture the name and email into a spreadsheet?

Ideally, the A column would capture the Name and B column would capture the email address.

Best Answer

You would need to write some sort of regex to extract that string from the message.

var threads = GmailApp.getInboxThreads();

for (var t in threads) {

  var messages = threads[t].getMessages[];
  var text = messages[0].getPlainBody();

  var matches = /From:(.*?)<(.*?)>/gi.exec(text);
  if (matches) 
      Logger.log(matches[1] + " :: " + matches[2]);

}