Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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.
ignoreEmbedded
(optional, boolean
)
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
)
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
)
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
)
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.getAttachments(): MessageFieldData[] | Attachment[]>
MessageFieldData
or Attachment
objects representing the email's attachments.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.
options
(optional, ParseOptions
): Options to modify the parsing behavior.Attachment
objects representing the embedded 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",
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox", "--disabled-setupid-sandbox"]
}
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.