How to turn email object into Stream, Buffer or String?

Asked

Viewed 129 times

0

I’m making an email application and my server is being made in Hapijs, and I’m having trouble taking the email and turning it into a String, Buffer or Stream.

I want to turn to these formats because it’s only the ones that Mailparser accepted, but I’m having some difficulties.

First I managed to do everything right, but for that I was creating TXT files and then reading them, and in both steps I used Fs. But I want to take the emails and parse them without creating any files, using Buffers, Streams or String that stay in memory until the parse is completed.

I’m already reading the documentation of Nodejs, Mailparser and Node-imap(that I’m using to get the emails, but I still can’t solve this problem...

  • How so get the emails? where are you receiving from? a request, from disk, another stream?

  • I’m receiving them from a test account that I created in gmail, using Node-imap, he makes the connection and after configuring the variables with login and password he searches the emails and can filter them through the flags, However, to be able to display these emails in my application I need to parse (with Mailparser) and I need to provide as input for parse a String, a Buffer or a Stream, and I want to do this without creating files on the disk, only save them in memory during the process. And I’m not managing to turn email into any of the three options I mentioned above...

1 answer

0


If the object email for 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=>{})

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

Should the return of node-imap be a matrix you must walk through it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.