@protontech/jsmimeparser
v3.0.1
Published
An asynchronous MIME parser written in JavaScript
Downloads
4,763
Keywords
Readme
This is a fork of mozilla-comm/jsmime that has been updated to include the changes made in mozilla/releases-comm-central (incl. UTF-7 support).
Further, the library now uses ES6 modules and exposes a user-friendly parseMail
function.
Code Layout
JSMime is a MIME parsing and composition library that is written completely in JavaScript using ES6 functionality and WebAPIs (where such APIs exist). There are a few features for which a standardized WebAPI does not exist; for these, external JavaScript libraries are used.
The MIME parser consists of three logical phases of translation:
- Build the MIME (and pseudo-MIME) tree.
- Convert the MIME tree into a list of body parts and attachments.
- Use the result to drive a displayed version of the message.
The first stage is located in rawMimeParser.js
, the second in mailParser.js
(in particular, the parseMail
function). The latter stage is left to the applications.
Install
npm i @protontech/jsmimeparser
Usage
The parseMail
function is designed to be user-friendly but remains bare-bones in the sense that it does not add metadata or information that is not found in the original message (e.g. no automatic contentID or checksum generation for the attachments, unlike Nodemailer's MailParser).
import { parseMail } from '@protontech/jsmimeparser';
const eml = `Message-Id: <[email protected]>
Received: from source ([69.9.251.177]) by exprod5mx37.postini.com ...
From: "Bob Example" <[email protected]>
To: "Alice Example" <[email protected]>
Date: Wed, 20 Aug 2003 16:02:43 -0500
Subject: Test message
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="XXXXboundary text"
This is a multipart message in MIME format.
--XXXXboundary text
Content-Type: text/plain
Hello Alice.
This is a test message with 5 lines in the message body
and an attachment.
Your friend,
Bob
--XXXXboundary text
Content-Type: image/gif
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=smile.gif
R0lGODlhyADIAMIAAP...+lmxwBLZ7FjJNkKsbcbyuGq0vKpH7bO50klqJ7YSmCYn4Yrrn4+elGsurYeoKy67e/ZqrrfogivvvONu4i6B8CJ6L77nguKigD0O7FK+mhhskoZIEhzwJwpjxLCFUy7co8ANH1xwxhY/LIpdIB/qmr6Hhvztfih+XPLKJ6c4HsYtK2ByvShb9UQCADs=
--XXXXboundary text--`
const {
attachments, // [{ contentType: 'image/gif', fileName: 'smile.gif', content: Uint8Array[71, 73, 70..], ... }]
body, // { text: 'Hello Alice.\nThis is..', html: '' }
subject, // 'Test message'
from, // // { name: 'Bob Example', email: '[email protected]' }
to, // [{ name: 'Alice Example', email: '[email protected]' }]
date, // Date('Wed, 20 Aug 2003 16:02:43 -0500')
...rest // headers and more
} = parseMail(eml);
See test/test_mail_parser.ts
for other examples with different MIME messages. Type information can be found in index.d.ts
.
Aside from parseMail
, several lower-level functions are exported by lib/jsmime
and lib/mailParser
(mostly unchanged from the original jsmime & mozilla repos).
Testing
Headless Chrome (or Chromium), Firefox and Webkit are used for the tests.
To install any missing browsers automatically, you can run npx playwright install-deps <chromium|firefox|webkit>
. Alternatively, you can install them manually as you normally would on your platform.
If you'd like to test on a subset of browsers, use e.g. npm test -- --browsers ChromeHeadless,FirefoxHeadless
.