Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
mail-export
Advanced tools
Parse .eml and .msg files or convert to pdf. Extract headers and attachments from .eml and msg files. Natively in typescript, support mjs & cjs!
Parse .eml/.msg files or convert to pdf, html, jpeg or png format, and extract attachments to files. Support typescript and javascript.
npm install mail-export
Note : The converter use puppeteer-html-pdf
, that uses puppeteer
as a dependency. As a result, you need your own chrome installation, and you can configure the cache as you want using puppeteer configuration
The following script works the same for eml & msg files. It will use the Converter class to convert into a readable stream, that will be used thereafter to create a pdf file.
import { normalize } from "node:path";
import { EmlParser, Convert } from "mail-export";
import fs from "node:fs"
const filePath = normalize("test_SA_3.eml"); //can also be a .msg file
const readable = fs.createReadStream(filePath)
const emlParser = await EmlParser.init(readable);
const html = await emailParser.getAsHtml();
if (!html) throw new Error("No message found");
const converter = new Convert(html);
await converter.createPdf("sample.pdf",{ format: "A4" });
import { createReadStream, createWriteStream, writeFileSync } from "node:fs";
import { normalize } from "node:path";
import { EmlParser, Convert } from "mail-export";
const filePath = normalize("test_SA.eml"); //or .msg file
const email = createReadStream(filePath);
const emailParser = EmlParser.init(email);
const attachments = emailParser.getAttachments({
ignoreEmbedded: false, //does nothing for .msg files
});
if (!attachments) throw new Error("No attachments found");
for (const attachment of attachments) {
//convert to file with attachment.content as Uint8Array
if (!attachment || !attachment.content || !attachment.filename) continue;
writeFileSync(attachment.filename, attachment.content);
}
It will successfully download the attachments to the current directory.
highlightKeywords
(optional, string[]
)
<mark></mark>
HTML tag.['foo', 'bar']
, the keywords foo
and bar
will be highlighted in the email HTML content.highlightCaseSensitive
(optional, boolean
)
formatEmailAddress
(optional, string
)
{{name}}
for the user name and {{address}}
for the email address.{{name}} <{{address}}>
will format the email address as John Doe <john.doe@mail.com>
.Includes all from ParseOptions
and adds the following:
ignoreEmbedded
(optional, boolean
)
excludeHeader
(optional, Partial<ExcludeHeaderEml>
)
Includes all from ParseOptions
and adds the following:
excludeHeader
(optional, Partial<ExcludeHeader>
)
The ExcludeHeader
type specifies options for excluding certain headers from the HTML output.
bcc
(boolean
)
bcc
header from the HTML output.cc
(boolean
)
cc
header from the HTML output.to
(boolean
)
to
header from the HTML output.from
(boolean
)
from
header from the HTML output.date
(boolean
)
date
header from the HTML output.subject
(boolean
)
subject
header from the HTML output.replyTo
(boolean
)
replyTo
header from the HTML output.attachments
(boolean
)
Only applicable for EML files.
embeddedAttachments
(boolean
)
The MessageFieldData
interface is an upgraded version of the FieldsData
interface from msgreader
. It is specifically used for handling data in the MSG format.
content
(optional, Uint8Array
)
Uint8Array
.htmlString
(optional, string
)
filename
(optional, string
)
The ParseOptions
interface provides configuration options for parsing EML and MSG files, and provide additional functionality such as keyword highlighting and header exclusion for the html output.
ignoreEmbedded
(optional, boolean
)
highlightKeywords
(optional, string[]
)
<mark></mark>
HTML tag.['foo', 'bar']
will highlight the keywords foo
and bar
in the email HTML content.highlightCaseSensitive
(optional, boolean
)
excludeHeader
(optional, Partial<ExcludeHeader>
)
The ExcludeHeader
interface specifies options for excluding certain headers from the HTML/PDF output.
bcc
(boolean
)cc
(boolean
)to
(boolean
)from
(boolean
)date
(boolean
)subject
(boolean
)replyTo
(boolean
)
attachments
(boolean
)embeddedAttachments
(boolean
)The MailAddress
interface represents a parsed email address in a digestible format.
name
(optional, string
)
address
(optional, string
)
The Header
interface represents the metadata associated with an email, including sender and recipient information, subject, date, and attachments.
subject
(optional, string
)
from
(optional, MailAddress[]
)
bcc
(optional, MailAddress[]
)
cc
(optional, MailAddress[]
)
to
(optional, MailAddress[]
)
replyTo
(optional, MailAddress[]
)
date
(optional, string | Date
)
Date
object.attachments
(optional, Attachment[] | MessageFieldData[]
)
Attachment
objects or MessageFieldData
objects.The Parser
interface provides methods to parse and extract information from email files, including headers, attachments, and content.
The EmlParser
and MessageParser
implements the interface Parser
.
EmlParser
: Used to parse EML files.
import { EmlParser } from "mail-export";
const readableStream = createReadStream("email.eml");
const emlParser = await EmlParser.init(readableStream);
MessageParser
: Used to parse MSG files.
import { MessageParser } from "mail-export";
const readableStream = createReadStream("email.msg");
const messageParser = await MessageParser.init(readableStream);
fileReadStream
(Readable
)
parsedMail
(MessageFieldData | ParsedMail
)
MessageFieldData
or ParsedMail
.options
(ParseOptions | undefined
)
[!NOTE]
ParseOptions
referee toEmlOptions
forEmlParser
andMessageOptions
forMessageParser
. The two options extendsParseOptions
and add specific options for each type of file.
init(fileReadStream?: Readable, options?: ParseOptions): Promise<EmlParser | MessageParser>
options
(optional, ParseOptions
): Options to modify the parsing behavior.fileReadStream
(optional, Readable
): The readable stream of the email file to be parsed.getHeader(): Header | undefined
Header
object containing the email's header information.getAsHtml(options?: ParseOptions): Promise<string | undefined> | string | undefined
options
(optional, ParseOptions
): Options to modify the parsing behavior.getBodyHtml(): string | undefined
The EmlParser
provides additional methods for parsing EML files.
Attachment
objects representing the embedded attachments.getAttachments(): MessageFieldData[] | Attachment[]
MessageFieldData
or Attachment
objects representing the email's attachments.The Convert
class provides methods to convert email content to PDF, buffer or Readable.
It uses, internally puppeteer-html-pdf
to convert the HTML content to PDF.
For option, you can refer to the puppeteer-html-pdf documentation.
Note : convertToStream
and convertToBuffer
remove the path
option if provided.
Default options:
{
format: "A4",
width: "21cm",
height: "29.7cm",
margin: {
top: "1.30cm",
left: "1.30cm",
right: "1.30cm",
bottom: "1.30cm",
},
printBackground: true,
preferCSSPageSize: true,
headless: true,
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disabled-setupid-sandbox",
"--font-render-hinting=none",
],
}
const converter = new Converter(html)
html
: stringconvertToStream(opt?: PuppeteerHTMLPDFOptions):Promise<Readable>
PuppeteerHTMLPDF
opt
(optional, PuppeteerHTMLPDFOptions
): Option for PuppeteerconvertToBuffer(opt?: PuppeteerHTMLPDFOptions):Promise<Buffer>
opt
(optional, PuppeteerHTMLPDFOptions
): Option for PuppeteercreatePdf(path: string, opt?: PuppeteerHTMLPDFOptions):Promise<void>
pdf
parameter.path
(string
) : Path to save the PDF.opt
(optional, PuppeteerHTMLPDFOptions
): Option for PuppeteerFAQs
Parse .eml and .msg files or convert to pdf. Extract headers and attachments from .eml and msg files. Natively in typescript, support mjs & cjs!
We found that mail-export demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.