I'm having serious problems decrypting the body of the messages I receive using the Gmail API. I want to capture the contents of a message and put the contents in a div. I use a base64 decoder, which, as I know, will not decode emails encoded differently, but I'm not sure how to check emails to decide which decoder to use - emails that say they are utf encoded -8, base64 successfully decoded, but not be a utf-8 decoder.
I have been studying email decryption for several days, and I found out that I am a bit out of my league here. I used to not do much work with email encoding. Here is the code I use to receive emails:
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 = '<i>Hello! I am reading your <b>inbox</b> emails.</i><br><br>------<br>'; var content = document.getElementById("message-list"); if (resp.messages == null) { content.innerHTML = "<b>Your inbox is empty.</b>"; } 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 += '<b>Subject: ' + header + '</b><br>'; content.innerHTML += '<b>From: ' + sender + '</b><br>'; content.innerHTML += contents + "<br><br>"; } catch (err) { console.log("Encoding error: " + encodings++); } }) }) } }); });
I did some checking and debugging, so console.log and some other things that are only for testing remained. However, you can see here what I'm trying to do.
What is the best way to decode email messages that I extract from the Gmail API? Should I try to put emails in <script> with the charset and type attributes matching the contents of the email encoding? I believe that I remember that charset only works with the src attribute, which I would not have. Any suggestions?
javascript email character-encoding decoding gmail-api
eugene1832
source share