Reputation: 8413
There's a few answers about this on StackOverflow, but none of them work for me.
I have mixed data coming from Google mailbox import (some are in utf-8, some are in base64) into my Node.Js app and I need to check for every string if it's in base64 or not.
Does anyone have a solution that works?
Data example
0KHQvdCw0YfQsNC70LAg0YHQvtC30LTQsNC10YLRgdGPINGA0LXRiNC10YLQutCwINC/0YDQvtGB
0YLRgNCw0L3RgdGC0LLQsCDQstC+0L7QsdGA0LDQttC10L3QuNGPLiZuYnNwOzxkaXY+PGJyPjwv
ZGl2PjxkaXY+0JfQsNGC0LXQvCDQvdCwINC90LXQtSDQvdCw0L3QuNC30YvQstCw0LXRgtGB0Y8g
0LLRi9C80YvRgdC10LsuPC9kaXY+PGRpdj48
Code to get it github.com/mscdex/node-imap but I keep only the message text, i.e.
msg.on('body', function(stream, info) {
stream.on('data', function(chunk) {
count += chunk.length;
buffer += chunk.toString('utf8');
}
}
Upvotes: 1
Views: 2525
Reputation: 8413
The problem is that data I was receiving via node-imap module of Node.Js contained two different encoding parameters in the params:
attrs.struct[0].params.charset
would be set as utf-8 encoding for all messages
while
attrs.struct[0].encoding
for the messages that were in base64 encoding would be set as BASE64
So I passed on the both parameters in variables to the msg.once('end')
, check if the attrs.struct[0].encoding is base64 and apply conversion for such strings from base64 to utf-8:
statement = new Buffer(email, 'base64').toString('utf8');
The rest get controlled through
statement = mimelib.decodeQuotedPrintable(email);
Upvotes: 1