简体   繁体   中英

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?

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 <group-digests@linkedin.com>
To: Arun Nagarajan <REMOVED@gmail.com>
Message-ID: <1440795364.35263280.1354293878345.JavaMail.app@ela4-app2521.prod>
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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM