Open tcurdt opened 9 years ago
+1
Messages from gmail have their bodies encoded in base64. I use this function to convert it to a normal string:
function base64toUTF8 (str) {
return new Buffer(str, 'base64').toString('utf8');
};
I wrote function
static getNestedBodyByMimetype(payload, type):string {
if(payload && payload.body.data && payload.mimeType === type) {
return Buffer.from(payload.body.data, 'base64').toString();
}
for(let part of payload.parts || []) {
const res = MailConverter.getNestedBodyByMimetype(part, type);
if(res) return res;
}
}
and use it in the following way
const text = MailConverter.getNestedBodyByMimetype(email.payload, 'text/plain'); // Buffer.from(email.payload.parts.find(part => part.mimeType === 'text/plain').body.data, 'base64').toString()
const html = MailConverter.getNestedBodyByMimetype(email.payload, 'text/html'); // Buffer.from(email.payload.parts.find(part => part.mimeType === 'text/html').body.data, 'base64').toString()
For email without text I can type
import {convert} from 'html-to-text';
const text = convert(html, {
wordwrap: false, selectors: [
{selector: 'img', format: 'skip'},
{selector: 'a', options: {noAnchorUrl: true}}
]
});
Fo emails without html I typed
const MarkdownIt = require('markdown-it');
const md = new MarkdownIt({linkify: true});
const html = md.render(text);
but this is out of scope of this package that only sync gmail raw data with us.
I got this to work but for multi part messages I see "part" objects - but don't see yet how to use them. For plain text the data is still encoded.
Would be great if you could expand on the currently very minimalistic example :)