EML file format
A pure Node.js library for parsing and building EML files, i.e. e-mail message format described in RFC 822. EML is returned by the POP3 protocol and handled by many e-mail agents like Mozilla Thunderbird or Micfosot Outlook. An EML file consists of headers and body similar to the HTTP structure.
File extension: .eml
Mime type: message/rfc822
How does EML look like?
Date: Wed, 29 Jan 2014 11:10:06 +0100
To: "Foo Bar" <foo.bar@example.com>
From: Online Shop <no-reply@example.com>
Subject: Winter promotions
Content-Type: text/plain; charset=utf-8
Lorem ipsum...
Getting Started
Setup
npm install -g eml-format
Read EML file
var fs = ;var emlformat = ; var eml = fs;emlformat;
Output structure
Command Line
A command line tool to extract .eml file into a folder. Output directory will be populated with .txt and .html message and attachment files.
Usage:
eml-unpack [options] [message.eml] [directory]
Options:
--help Print this message
--verbose Enable detailed logging
--version Print version number
--json Create parsed.json and manifest.json
--no-unpack Used with --json to skip unpacking
Examples:
eml-unpack message.eml .
eml-unpack --verbose sample.eml folder
eml-unpack --json --no-unpack ./sample.eml ./folder
Reference
read(eml, [options], callback)
Parses EML file content and return user-friendly object.
Argument | Type | Description |
---|---|---|
eml | string or object | EML file content or object from 'parse' |
options | object | Optional parameter, { headersOnly: true } (false by default) |
callback | function(error, data) | Callback function to be invoked when read is complete |
parse(eml, [options], callback)
Parses EML file content and returns object-oriented representation of the content.
Argument | Type | Description |
---|---|---|
eml | string | EML file content |
options | object | Optional parameter, { headersOnly: true } (false by default) |
callback | function(error, data) | Callback function to be invoked when parse is complete |
build(eml, callback)
Builds an EML message.
Argument | Type | Description |
---|---|---|
data | object | E-mail data, see example |
callback | function(error, eml) | Callback function to be invoked when build is complete |
unpack(eml, directory, callback)
Unpacks EML message and attachments to a directory.
Argument | Type | Description |
---|---|---|
eml | string or object | EML file content or object from 'parse' |
directory | string | Folder name or directory path where to unpack |
callback | function(error, data) | Callback function to be invoked when read is complete |
Examples
Read headers only
var fs = ;var emlformat = ; var eml = fs;emlformat;
Read the complete EML file
The parse
function parses raw EML content into JavaScript object for further processing.
var fs = ;var emlformat = ; var eml = fs;emlformat;
Or use the read
instead of parse parse
. The read
function decodes the base64, quote-printable, =?UTF-8?...?= encoded content and extracts plain text, html content and attachments. So this method is a little slower but more user friendly.
emlformat;
Unpack files from an EML file
Extracts plain text, html content and attachments to a directory
var fs = ;var emlformat = ; var dir = "unpacked"; //Output directoryvar eml = fs;emlformat;
Create an EML file
var fs = ;var emlformat = ; var data = from: "no-reply@bar.com" to: name: "Foo Bar" email: "foo@bar.com" subject: "Winter promotions" text: "Lorem ipsum..." html: '<html><head></head><body>Lorem ipsum...<br /><img src="nodejs.png" alt="" /></body></html>' attachments: name: "sample.txt" contentType: "text/plain; charset=utf-8" data: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi eget elit turpis. Aliquam lorem nunc, dignissim in risus at, tempus aliquet justo..." name: "nodejs.png" contentType: "image/png" data: fs inline: true ; var eml = fs;emlformat;
Register a new mime type file extension
var emlformat = ;emlformatfileExtensions"application/zip" = ".zip";emlformatfileExtensions"application/octet-stream" = ".bin";
Extract e-mail address and name
Plain text name
var emlformat = ;var data = emlformat;//data.name == "Foo Bar";//data.email == "foo@bar.com";
UTF-8 encoded name
var emlformat = ;var data = emlformat;//data.name == "You’re Foo Bar";//data.email == "foo@bar.com";
Decode "quoted-printable"
var emlformat = ;var message = emlformat;
Decode "=?UTF-8?...?=" string
var emlformat = ;var message = emlformat;