Giter VIP home page Giter VIP logo

eml-parser's Introduction

Eml-Parser

Parse .eml files or convert to pdf, html, jpeg or png format. Extract headers and attachments from .eml files.

installation

npm i eml-parser --save

Quick Start

const  EmlParser = require('eml-parser');
const  fs = require('fs');

let  emailFile = fs.createReadStream('./test.eml');

new  EmlParser(emailFile).convertEmailToStream('pdf')
.then(stream  => {
   stream.pipe(fs.createWriteStream(emailFile.path + '.pdf'));
})
.catch(err  => {
   console.log(err);
})

Reference

Class: EmlParser

new EmlParser(fs.createReadStream('test.eml'))

constructor takes a Read Stream as input

Methods

parseEml

new EmlParser(fs.createReadStream('test.eml'))
.parseEml()
.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);
})

getEmailHeaders

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);
})

getEmailBodyHtml

returns email content as a html string (without headers like subject, from, etc)

new EmlParser(fs.createReadStream('test.eml'))
.getEmailBodyHtml()
.then(htmlString  => {
	fs.writeFileSync('abc.html',htmlString)	;
})
.catch(err  => {
	console.log(err);
})

getEmailAsHtml

returns whole email as a html string (including headers like subject, from, etc)

new EmlParser(fs.createReadStream('test.eml'))
.getEmailAsHtml()
.then(htmlString  => {
	fs.writeFileSync('abc.html',htmlString)	;
})
.catch(err  => {
	console.log(err);
})

convertEmailToStream

takes 3 optional arguments

  • type:'pdf'|'jpeg'|'png', defaults to: 'pdf'
  • orientation:'potrait'|'landscape', defaults to: 'landscape'
  • format:'A3'|'A4'|'A5'|'Legal'|'Letter'|'Tabloid' returns a stream which can be piped to a Write Stream to write to a file.
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);
})

convertEmailToBuffer

takes 3 optional arguments:

  • type:'pdf'|'jpeg'|'png', defaults to: 'pdf'
  • orientation:'potrait'|'landscape', defaults to: 'landscape'
  • format:'A3'|'A4'|'A5'|'Legal'|'Letter'|'Tabloid' returns a buffer object which can be used to write to a file using fs.write.
let  file = fs.createReadStream('test.eml')
new  EmlParser(file)
.convertEmailToBuffer('png')
.then(buffer  => {
	//use fs.write to write into file
})
.catch(err  => {
	console.log(err);
})

getEmailAttachments

let  file = fs.createReadStream('test.eml')
new  EmlParser(file)
.getEmailAttachments()
.then(attachments  => {
	attachments.forEach(attch  => {
		//attch.content is the buffer object
		console.log(attch.filename, attach.content);
		.then(res  => {
			console.log(res);
		})
		.catch(err  => {
		console.log(err);
		})
	});
})
.catch(err  => {
	console.log(err);
})

eml-parser's People

Contributors

ankit1329 avatar dependabot[bot] avatar jfvanin avatar skempin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.