### Install mailsplit Source: https://github.com/zone-eu/mailsplit/blob/master/README.md Install the mailsplit module using npm. This command adds mailsplit as a dependency to your project. ```bash npm install mailsplit --save ``` -------------------------------- ### Pipe message stream through Splitter and Joiner Source: https://github.com/zone-eu/mailsplit/blob/master/README.md This example demonstrates how to pipe a message stream through the MailSplit Splitter and Joiner to process and output the message to stdout. Ensure you have the 'mailsplit' module installed. ```javascript let Splitter = require('mailsplit').Splitter; let Joiner = require('mailsplit').Joiner; let splitter = new Splitter(); let joiner = new Joiner(); // pipe a message source to splitter, then joiner and finally to stdout someMessagStream .pipe(splitter) .pipe(joiner) .pipe(process.stdout); ``` -------------------------------- ### Handle Parsed Email Data Source: https://github.com/zone-eu/mailsplit/blob/master/README.md Process the data emitted by the Splitter stream. This example demonstrates how to handle 'node', 'data', and 'body' types to extract headers, multipart structures, and leaf element bodies. ```javascript let Splitter = require('mailsplit').Splitter; let splitter = new Splitter(); // handle parsed data splitter.on('data', data => { switch (data.type) { case 'node': // node header block process.stdout.write(data.getHeaders()); break; case 'data': // multipart message structure // this is not related to any specific 'node' block as it includes // everything between the end of some node body and between the next header process.stdout.write(data.value); break; case 'body': // Leaf element body. Includes the body for the last 'node' block. You might // have several 'body' calls for a single 'node' block process.stdout.write(data.value); break; } }); // send data to the parser someMessagStream.pipe(splitter); ``` -------------------------------- ### Initialize Splitter Stream Source: https://github.com/zone-eu/mailsplit/blob/master/README.md Initialize a new Splitter stream instance. Options can be provided to configure parsing behavior, such as ignoring embedded messages or limiting header size. ```javascript let Splitter = require('mailsplit').Splitter; let splitter = new Splitter(options); ``` -------------------------------- ### Stream Specific JPEG Nodes from Emails Source: https://github.com/zone-eu/mailsplit/blob/master/README.md Utilize Streamer to extract nodes matching a filter function. Pipe a Splitter stream into Streamer and its output to a Joiner. The Streamer emits a 'node' event for matching nodes, providing a decoder for content and a done callback. Ensure data.done() is called after processing. ```javascript let Splitter = require('mailsplit').Splitter; let Joiner = require('mailsplit').Joiner; let Streamer = require('mailsplit').Streamer; let fs = require('fs'); let splitter = new Splitter(); let joiner = new Joiner(); let streamer = new Streamer(node => node.contentType === 'image/jpeg'); streamer.on('node', data => { // write to file data.decoder.pipe(fs.createWriteStream(data.node.filename || 'image.jpg')); data.done(); }); // pipe a message source to splitter, then streamer, then joiner and finally to stdout someMessagStream .pipe(splitter) .pipe(streamer) .pipe(joiner) .pipe(process.stdout); ``` -------------------------------- ### Rewrite Specific HTML Nodes in Emails Source: https://github.com/zone-eu/mailsplit/blob/master/README.md Use Rewriter to modify nodes matching a filter function. Pipe a Splitter stream into Rewriter and its output to a Joiner. The Rewriter emits a 'node' event for matching nodes, providing access to the node, a decoder for its content, and an encoder to write modified content. ```javascript let Splitter = require('mailsplit').Splitter; let Joiner = require('mailsplit').Joiner; let Rewriter = require('mailsplit').Rewriter; let splitter = new Splitter(); let joiner = new Joiner(); let rewriter = new Rewriter(node => node.contentType === 'text/html'); rewriter.on('node', data => { // manage headers with node.headers node.headers.add('X-Processed-Time', new Date.toISOString()); // do nothing, just reencode existing data data.decoder.pipe(data.encoder); }); // pipe a message source to splitter, then rewriter, then joiner and finally to stdout someMessagStream .pipe(splitter) .pipe(rewriter) .pipe(joiner) .pipe(process.stdout); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.