Weehooey
Weehooey

Reputation: 1008

Read Email Headers Using Google Apps Script

I would like to read through my all my Gmail emails' headers to find specific information.

I know there is no access through the GmailApp service (well, pretty sure anyway).

Any ideas on how to get the header information with a solution primarily based in Apps Script?

Upvotes: 4

Views: 5126

Answers (2)

Roy Golan
Roy Golan

Reputation: 143

Thanks @Arun, just to add a small example on top of yours, to get for example the 'X-LinkedIn-Template' header value:

var header = message.getRawContent().match(/(X-LinkedIn-Template:)(.*)/);
header[0] // the whole match == X-LinkedIn-Template: anet_digest_type
header[1] // the header key  == X-LinkedIn-Template
header[2] // the value       == anet_digest_type

Upvotes: 1

Arun Nagarajan
Arun Nagarajan

Reputation: 5645

No, email headers are not possible through the Apps Script services. You'll have to go the IMAP or SMTP route for that.

-- UPDATE

You got me curious and looks like you can get the important ones through getRawContent() and manually parse it.

Here is the code you can try -

function processInbox() {
  //get first message in first thread
  var message = GmailApp.getInboxThreads(0,1)[0].getMessages()[0];
  Logger.log(message.getRawContent());
};

and here is the output from a LinkedIn group message -

From: Google APPS users Group Members <[email protected]>
To: Arun Nagarajan <[email protected]>
Message-ID: <[email protected]>
Subject: [2] discussions, [1] comment and [1] job on LinkedIn
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_35263277_1178500841.1354293878345"
X-LinkedIn-Template: anet_digest_type
X-LinkedIn-Class: GROUPDIGEST
X-LinkedIn-fbl: s-uPmFAdhOYxvH52TwUlkvTF6rOfu4R6CRfjIFaaCOYfXQgGt9OunBRp

------=_Part_35263277_1178500841.1354293878345
Content-Type: multipart/related; 
    boundary="----=_Part_35263278_821958406.1354293878345"

------=_Part_35263278_821958406.1354293878345
Content-Type: multipart/alternative; 
    boundary="----=_Part_35263270_1718315066.1354293878331"

------=_Part_35263270_1718315066.1354293878331
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

message here
<snip>

Upvotes: 12

Related Questions