Gmail API - analysis message content (Base64 decoding?) Using Javascript

I am trying to use the Gmail API to receive a user’s email, capture the subject and body of a message, and then display it on a web page. I will do other things, but this is the part that is difficult for me. I am using Angular.js.

Here is my API call:

function makeApiCall() { gapi.client.load('gmail', 'v1', function() { var request = gapi.client.gmail.users.messages.list({ labelIds: ['INBOX'] }); request.execute(function(resp) { var content = document.getElementById("message-list"); angular.forEach(resp, function(message) { var email = gapi.client.gmail.users.messages.get({'id': message.id}); // var raw = email.payload.parts; // console.log(raw); content.innerHTML += JSON.stringify(email) + "<br>"; }) }); }); } 

So gapi.client.gmail.users.messages.list returns an array of my messages with their identification numbers. It works.

Calling gapi.client.gmail.users.messages.get({<specific message ID>}) calls this - {"B":{"method":"gmail.users.messages.get","rpcParams":{},"transport":{"name":"googleapis"}}} .

Not sure what it is, but trying to get the message payload ( email.payload.parts ) leads to undefined . So how can I get the content of the message?

In addition, I would suggest that if I could get the contents of the message, I would have to Base64 decode the contents to extract some English from it. Any suggestions for this would also be helpful. I found this: https://github.com/kvz/phpjs , but since I'm not sure how to go about getting the contents of the message so that I can decode it, so it’s not sure if this php.js helps in this regard.

+7
javascript angularjs base64 gmail-api
source share
4 answers

Depending on how your emails look (individual text / plain parts? Multipart with text / html attachments? Etc.), you may or may not have any “parts” in your .payload email, and instead, you will have what you are looking for in "email.payload.body.data" (for single-part messages). It is all assumed that you are doing message.get with a standard format ("full"). If you want to receive all the email in the message.raw field and process it in electronic libraries for your language, you can call message.get (format = raw).

For more information, check out the “body” and “parts []” field documentation for “Message” at https://developers.google.com/gmail/api/v1/reference/users/messages

+3
source share

As for Base64 decoding, you can use

 atob(dataToDecode) 

For Gmail, you'll also want to replace some characters:

 atob( dataToDecode.replace(/-/g, '+').replace(/_/g, '/') ); 

The above function is available to you in JavaScript (see ref ). I myself use it to decode Gmail messages. No need to install unnecessary things. As an interesting tangent, if you want to encode your message in Base64, use btoa.

Now, to access the payload of your message, you can write a function:

 var extractField = function(json, fieldName) { return json.payload.headers.filter(function(header) { return header.name === fieldName; })[0].value; }; var date = extractField(response, "Date"); var subject = extractField(response, "Subject"); 

referring to the previous SO question and

 var part = message.parts.filter(function(part) { return part.mimeType == 'text/html'; }); var html = atob(part.body.data.replace(/-/g, '+').replace(/_/g, '/')); 
+22
source share

Oh! I understood. parts is an array, so I would have to call it the following: gapi.client.gmail.users.messages.get({'id': <message ID>}).payload.parts[0].body.data

Now my problem is to decrypt emails that have been successful in text email messages, but not email from non-personal places (enterprises, social networks, email updates, etc.). But I will ask a new question to get answers to this.

+3
source share

You need to find where the body is for a given mime type, I wrote a recursive function for this:

 function searchBodyRec(payload, mimeType){ if (payload.body && payload.body.size && payload.mimeType === mimeType) { return payload.body.data; } else if (payload.parts && payload.parts.length) { return payload.parts.flatMap(function(part){ return searchBodyRec(part, mimeType); }).filter(function(body){ return body; }); } } 

So now you can call

 var encodedBody = searchBodyRec(this.message.payload, 'text/plain'); 

See the flatMap method? The classic FP method is missing in js, here's how to add it (or you can use lodash.js or underscore.js if you don't want to communicate with your own objects)

 Array.prototype.flatMap = function(lambda) { return Array.prototype.concat.apply([], this.map(lambda)); }; 
0
source share

All Articles