Gmail API - Parse message content (Base64 decoding?) with Javascript

前端 未结 4 755
野的像风
野的像风 2020-12-14 13:15

I\'m trying to use the Gmail API to get a user\'s email, grab the message subject and body, and then display it on a webpage. I\'ll be doing other stuff with it, but this is

相关标签:
4条回答
  • 2020-12-14 13:35

    Ah! I figured it out. parts is an array, so I should have been calling it like: gapi.client.gmail.users.messages.get({'id': <message ID>}).payload.parts[0].body.data

    Now my problem is decoding the emails, which is proving successful in plain text emails, but failing in emails from non-personal locations (businesses, social media update emails, etc.). But I'll make a new question to get answers for that.

    0 讨论(0)
  • 2020-12-14 13:39

    You need to search where the body for a given mime type is, I have written a recursive function for that:

    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 up there? Classic FP method missing in js, here is how to add it (or you can use lodash.js, or underscore.js if you don't want to mess with the native objects)

    Array.prototype.flatMap = function(lambda) { 
        return Array.prototype.concat.apply([], this.map(lambda)); 
    };
    
    0 讨论(0)
  • 2020-12-14 13:43

    Depending on what your emails look like (single text/plain part? multipart with text/html? attachments, etc?) you may or may not have any "parts" in your email.payload and instead you'll have what you're looking for in "email.payload.body.data" (for single-part messages). This is all assuming you're doing a message.get with the default format ("full"). If you instead want to get the entire email in the message.raw field and deal with it in email libraries for your language you can call message.get(format=raw).

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

    0 讨论(0)
  • 2020-12-14 13:43

    Regarding the 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 use it myself to decode the Gmail messages. No need to install extra stuff. As an interesting tangent, if you want to encode your message to Base64, use btoa.

    Now, for accessing your message payload, 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");
    

    referenced from my 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, '/'));
    
    0 讨论(0)
提交回复
热议问题