title: Gmail API decoding messages in Javascript
body: <p>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 base64 decoder, which I know won't decode emails encoded differently, but I am not sure how to check an email to decide which decoder to use -- emails that say they are utf-8 encoded are successfully decoded by the base64 decoder, but not be a utf-8 decoder.</p>
  
  <p>I've been researching email decoding for several days now, and I've learned that I am a little out of my league here. I haven't done much work with coding around email before. Here is the code I am using to get the emails:</p>
  
  <pre><code>gapi.client.load('gmail', 'v1', function() {
  var request = gapi.client.gmail.users.messages.list({
    labelIds: ['INBOX']
  });
  request.execute(function(resp) {
    document.getElementById('email-announcement').innerHTML = '&lt;i&gt;Hello! I am reading your &lt;b&gt;inbox&lt;/b&gt; emails.&lt;/i&gt;&lt;br&gt;&lt;br&gt;------&lt;br&gt;';
    var content = document.getElementById("message-list");
    if (resp.messages == null) {
      content.innerHTML = "&lt;b&gt;Your inbox is empty.&lt;/b&gt;";
    } else {
      var encodings = 0;
      content.innerHTML = "";
      angular.forEach(resp.messages, function(message) {
        var email = gapi.client.gmail.users.messages.get({
        'id': message.id
        });
        email.execute(function(stuff) {
          if (stuff.payload == null) {
            console.log("Payload null: " + message.id);
          }
          var header = "";
          var sender = "";
          angular.forEach(stuff.payload.headers, function(item) {
            if (item.name == "Subject") {
              header = item.value;
            }
            if (item.name == "From") {
              sender = item.value;
            }
          })
          try {
            var contents = "";
            if (stuff.payload.parts == null) {
              contents = base64.decode(stuff.payload.body.data);
            } else {
              contents = base64.decode(stuff.payload.parts[0].body.data);
            }
            content.innerHTML += '&lt;b&gt;Subject: ' + header + '&lt;/b&gt;&lt;br&gt;';
            content.innerHTML += '&lt;b&gt;From: ' + sender + '&lt;/b&gt;&lt;br&gt;';
            content.innerHTML += contents + "&lt;br&gt;&lt;br&gt;";
          } catch (err) {
            console.log("Encoding error: " + encodings++);
          }
        })
      })
    }
   });
  });
  </code></pre>
  
  <p>I was performing some checks and debugging, so there's leftover <code>console.log</code>'s and some other things that are only there for testing. Still, you can see here what I am trying to do. </p>
  
  <p>What is the best way to decode the emails I pull from the Gmail API? Should I try to put the emails into <code>&lt;script&gt;</code>'s with <code>charset</code> and <code>type</code> attributes matching the encoding content of the email? I believe I remember charset only works with a <code>src</code> attribute, which I wouldn't have here. Any suggestions?</p>