Gmail API decoding messages in Javascript

后端 未结 7 990
暖寄归人
暖寄归人 2020-12-30 11:07

I am having serious problems decoding the message body of the emails I get using the Gmail API. I want to grab the message content and put the content in a div. I am using a

7条回答
  •  攒了一身酷
    2020-12-30 11:32

    Use atob to decode the messages in JavaScript (see ref). 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);
    

    If the above does not decode 100% properly, the comments by @cgenco on this answer below may apply to you. In that case, do

    var html = atob(part.body.data.replace(/-/g, '+').replace(/_/g, '/'));
    

提交回复
热议问题