Advanced email parser for Node.js. Everything is handled as a stream which should make it able to parse even very large messages (100MB+) with relatively low overhead.
The module exposes two separate modes, a lower level
MailParser class and
simpleParser function. The latter is simpler to use (hence the name) but is less resource efficient as it buffers attachment contents in memory.
For older version of MailParser see the docs here
This module is part of the Nodemailer bundle. Starting from v2.0.0 MailParser is licensed under the European Union Public License 1.1. In general, EUPLv1.1 is a copyleft license compatible with GPLv2, so if you're OK using GPL then you should be OK using MailParser. Previous versions of MailParser are licensed under the MIT license.
npm install mailparser
const simpleParser = simpleParser;
or as a Promise:
Address objects have the following structure:
value an array with address details
text is a formatted address string for plaintext context
html is a formatted address string for HTML context
value:address: 'email@example.com'name: 'Andris Reinman'address: 'firstname.lastname@example.org'name: ''html: '<span class="mp_address_name">Andris Reinman</span> <<a href="" class="mp_address_email">email@example.com</a>>, <a href="" class="mp_address_email">firstname.lastname@example.org</a>'text: 'Andris Reinman <email@example.com>, firstname.lastname@example.org'
headers is a Map with lowercase header keys. So if you want to check for the Subject: header then you can do it like this:
The format of a header depends on the specific key. For most header keys the value is either a string (a single header) or an array of strings (multiple headers with the same key were found).
Special header keys are the following:
references is a string if only a single reference-id exists or an array if multiple ids exist
date value is a Date object
The following headers are parsed into structured objects, where
value property includes the main value as string and
params property holds an object of additional arguments as key-value pairs
Some headers are also automaticaly mime-word decoded
Attachment objects have the following structure:
MailParser is a lower-level email parsing class. It is a transform stream that takes email source as bytestream for the input and emits data objects for attachments and text contents.
const MailParser = MailParser;let parser =
The parser emits 'headers' once message headers have been processed. The headers object is a Map. Different header keys have different kind of values, for example address headers have the address object/array as the value while subject value is string.
Header keys in the Map are lowercase.
Event 'data' or 'readable' emits message content objects. The type of the object can be determine by the
type property. Currently there are two kind of data objects
Attachment object is the same as in
simpleParser except that
content is not a buffer but a stream. Additionally there's a method
release() that must be called once you have processed the attachment. The property
related is set after message processing is ended, so at the
data event this value is not yet available.
If you do not call
release() then the message processing is paused.
Text object has the following keys:
Charset decoding is handled using iconv-lite that is missing some charsets, especially some Japanese ones. If required then it would be possible to switch to native iconv bindings with node-iconv to handle these missing charsets but for now this option is not used for easier packaging.
European Union Public License 1.1. Commercial licenses available upon request. Contact email@example.com for details.
© 2017 Kreata OÜ