node-email
node-email copied to clipboard
Streams!
- I want to stream the body of a message, rather than making it a string.
- I want to stream attachments to a message by just hooking up a FileReadStream.
Maybe the email message object could be a Stream object, and just alias the end() function to send() for coolness.
What do you think of this for an API shape?
var email = new Email()
email.from = "[email protected]"
email.to = "[email protected]"
email.subject = "Dja Get That Thing I Sentcha?"
// email object is a write stream, so pipe/sys.pump to it
// whatever gets pumped in is the message body.
fs.createReadStream("message-body").pipe(email)
// attachment stream
var txtAttachment = email.attachment()
txtAttachment.mimeType = "text/plain"
txtAttachment.name = "some-file.txt"
fs.createReadStream("some-file.txt").pipe(txtAttachment)
// or maybe something like this?
var att = email.attach({"type":"image/png", "filename":"image.png")
fs.createReadStream("image.png", att)
// or, even infer the name and type and set up the stream for me:
email.attach("image.png")
Streams would be nice Isaac. The api looks pretty good overall.
Email being a WriteStream for the message body is cool. I would probably just add a flag to turn off auto sending on body stream complete in case they want to do something later before sending.
Fleshing it out a bit:
// create the stream and infer the mime type and name
email.attach(path)
// accept a ReadStream and infer the mime type from the name
email.attach(ReadStream, "img.png")
// accept a ReadStream and optionally allow specifying mimeType
email.attach(ReadStream, { name: "img.png" [, type: "image/png"] })
// if passed options first returns a writable stream
var att = email.attach({ name: "img.png" [, type: "image/png"] })
var stream = fs.createReadStream("msg.txt").pipe(att)
Not sure if we'd need to fire an event to folks know their streams are complete? But either way, I think we should be able to send() without caring and it just wait til all streams complete.