How to transform mail object into Stream, Buffer or String?

0

I'm doing an email application and my server is being done in HapiJS, and I'm having trouble picking up the email and turning it into a String, Buffer, or Stream.

I want to transform to these formats because they are the only ones that MailParser accept, but I have some difficulties.

First of all I was able to do everything right, but for this I was creating TXT files and then reading them, and in both steps I used fs. However I want to get the emails and parse them without creating any files, using Buffers, Streams or String that remain in memory until the parse is finished.

I'm already reading the NodeJS, MailParser, and node-imap documentation (which I'm using to get the emails , but I still can not solve this problem ...

    
asked by anonymous 28.08.2017 / 21:10

1 answer

0
If the email object is literally a object javascript (in memory) you can convert it to string using JSON and then use Buffer :

const simpleParser = require('mailparser').simpleParser;

let object_example = {
    test: true
}
// convert to string
let mailBuffer = Buffer.from(JSON.stringify(object_test), 'utf-8')
// as a Promise
simpleParser(mailBuffer).then(mail=>{}).catch(err=>{})

As you are searching with node-imap and "filtering" I soon believe (for not using node-imap ) that these returns are in memory ( arrays or object ). Just use the logic above, just replace "object_example" with the item returned by node-imap .

If the return of node-imap is an array you must go through it.

    
28.08.2017 / 21:58