
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
eml-parser
Advanced tools
Parse .eml and .msg files or convert to pdf, html, jpeg or png format. Extract headers and attachments from .eml and msg files.
npm i eml-parser --save
const EmlParser = require('eml-parser');
const fs = require('fs');
let emailFile = fs.createReadStream('./test.eml'); // or test.msg
new EmlParser(emailFile).convertEmailToStream('pdf')
.then(stream => {
stream.pipe(fs.createWriteStream(emailFile.path + '.pdf'));
})
.catch(err => {
console.log(err);
})
new EmlParser(emailFile).convertMessageToStream('pdf')
.then(stream => {
stream.pipe(fs.createWriteStream(emailFile.path + '.pdf'));
})
.catch(err => {
console.log(err);
})
{highlightKeywords: String[], highlightCaseSensitive: true| undefined}
to highlight keywords provided in highlightKeywords
option, highlightCaseSensitive: true
will do case sensitive match to highlight keywords. Works with all functions which return email content in any format (html, pdf, image, etc).new EmlParser(fs.createReadStream('test.eml'))
constructor takes a Read Stream as input
takes 2 optional arguments, returns the parsed eml object.
{ignoreEmbedded: true}
, use this to ignore embedded files from appearing under attachments{highlightKeywords: String[], highlightCaseSensitive: true}
, (e.g: {highlightKeywords: ["foo", "bar"], highlightCaseSensitive: true}) use this to highlight certain keywords in the email's html content. highlightCaseSensitive: true
will highlight keywords which match the case, defaults to false.new EmlParser(fs.createReadStream('test.eml'))
.parseEml(options?)
.then(result => {
// properties in result object:
// {
// "attachments": [],
// "headers": {},
// "headerLines": [],
// "html": "",
// "text": "",
// "textAsHtml": "",
// "subject": "",
// "references": "",
// "date": "",
// "to": {},
// "from": {},
// "cc": {},
// "messageId": "",
// "inReplyTo": ""
// }
console.log(result);
})
.catch(err => {
console.log(err);
})
new EmlParser(fs.createReadStream('test.eml'))
.getEmailHeaders()
.then(headers => {
//properties of headers object
//{
// subject: result.subject,
// from: result.from.value,
// to: result.to.value,
// cc: result.cc.value,
// date: result.date,
// inReplyTo: result.inReplyTo,
// messageId: result.messageId
//}
console.log(headers)
})
.catch(err => {
console.log(err);
})
takes 1 optional argument, returns email content as a html string (without headers like subject, from, etc).
{highlightKeywords: String[], highlightCaseSensitive: true}
, (e.g: {highlightKeywords: ["foo", "bar"], highlightCaseSensitive: true}) use this to highlight certain keywords in the email's html content. highlightCaseSensitive: true
will highlight keywords which match the case, defaults to false.new EmlParser(fs.createReadStream('test.eml'))
.getEmailBodyHtml()
.then(htmlString => {
fs.writeFileSync('abc.html',htmlString) ;
})
.catch(err => {
console.log(err);
})
takes 1 optional argument, returns whole email as a html string (including headers like subject, from, etc).
{highlightKeywords: String[], highlightCaseSensitive: true}
, (e.g: {highlightKeywords: ["foo", "bar"], highlightCaseSensitive: true}) use this to highlight certain keywords in the email's html content. highlightCaseSensitive: true
will highlight keywords which match the case, defaults to false.new EmlParser(fs.createReadStream('test.eml'))
.getEmailAsHtml()
.then(htmlString => {
fs.writeFileSync('abc.html',htmlString) ;
})
.catch(err => {
console.log(err);
})
takes 4 optional arguments, returns a stream which can be piped to a Write Stream to write to a file.
type:'pdf'|'jpeg'|'png'
, defaults to: 'pdf'orientation:'potrait'|'landscape'
, defaults to: 'landscape'format:'A3'|'A4'|'A5'|'Legal'|'Letter'|'Tabloid'
{highlightKeywords: String[], highlightCaseSensitive: true}
, (e.g: {highlightKeywords: ["foo", "bar"], highlightCaseSensitive: true}) use this to highlight certain keywords in the email's html content. highlightCaseSensitive: true
will highlight keywords which match the case, defaults to false.let file = fs.createReadStream('test.eml')
new EmlParser(file)
.convertEmailToStream('png')
.then(stream => {
stream.pipe(fs.createWriteStream(file.path + '.png'));
})
.catch(err => {
console.log(err);
})
takes 4 optional arguments, returns a buffer object which can be used to write to a file using fs.write.:
type:'pdf'|'jpeg'|'png'
, defaults to: 'pdf'orientation:'potrait'|'landscape'
, defaults to: 'landscape'format:'A3'|'A4'|'A5'|'Legal'|'Letter'|'Tabloid'
{highlightKeywords: String[], highlightCaseSensitive: true}
, (e.g: {highlightKeywords: ["foo", "bar"], highlightCaseSensitive: true}) use this to highlight certain keywords in the email's html content. highlightCaseSensitive: true
will highlight keywords which match the case, defaults to false.let file = fs.createReadStream('test.eml')
new EmlParser(file)
.convertEmailToBuffer(null, null, null, { highlightKeywords: ['foo', 'bar', 'baz', 'foo baz'], highlightCaseSensitive: true })
.then(buffer => {
//use fs.write to write into file
})
.catch(err => {
console.log(err);
})
takes 1 optional argument, returns the list of attachments:
{ignoreEmbedded: true}
, defaults to falselet file = fs.createReadStream('test.eml')
new EmlParser(file)
.getEmailAttachments(options?) //options: {ignoreEmbedded: true} to ignore embedded files
.then(attachments => {
attachments.forEach(attachment => {
//attachment.content is the buffer object
console.log(attachment.filename, attachment.content);
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
})
});
})
.catch(err => {
console.log(err);
})
returns the list of only embedded files
let file = fs.createReadStream('test.eml')
new EmlParser(file)
.getEmailEmbeddedFiles()
.then(embeddedFiles => {
embeddedFiles.forEach(embed => {
//embed.content is the buffer object
console.log(embed.filename, embed.content);
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
})
});
})
.catch(err => {
console.log(err);
})
new EmlParser(fs.createReadStream('test.msg'))
.parseMsg(options?)
.then(result => {
// properties in result object:
// {
// "dataType": "msg",
// "attachments": [],
// "recipients": [
// {
// "dataType": "recipient",
// "addressType": "",
// "name": "",
// "email": "",
// "smtpAddress": "",
// "recipType": "to"
// },
// {
// "dataType": "recipient",
// "addressType": "",
// "name": "",
// "email": "",
// "smtpAddress": "",
// "recipType": "cc"
// }
// ],
// "messageClass": "",
// "sentRepresentingSmtpAddress": "",
// "lastModifierSMTPAddress": "",
// "inetAcctName": "",
// "subject": "",
// "conversationTopic": "",
// "normalizedSubject": "",
// "body": "",
// "lastModifierName": "",
// "senderSmtpAddress": "",
// "creatorSMTPAddress": "",
// "creationTime": "",
// "lastModificationTime": "",
// "clientSubmitTime": "",
// "messageDeliveryTime": "",
// "messageFlags": 0,
// "internetCodepage": 0,
// "messageLocaleId": 0,
// "messageCodepage": 0,
// "headers": "",
// "senderName": "",
// "senderEmail": "",
// "senderAddressType": "",
// "html": ""
// }
console.log(result);
})
.catch(err => {
console.log(err);
})
FAQs
Parse .eml and .msg files or convert to pdf, html, jpeg or png format. Extract headers and attachments from .eml and msg files.
The npm package eml-parser receives a total of 5,006 weekly downloads. As such, eml-parser popularity was classified as popular.
We found that eml-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.