Library to extract and decode message content from single and multi-part email bodies.
npm install mail-body-parser$ npm install mail-body-parser
javascript
const mailBodyParser = require("mail-body-parser");
`
or if you like destructuring:
`javascript
const { parseBody } = require("mail-body-parser");
`
Basic Use
Pass a boundary, header, and message to the parseBody function. If the message is not multipart, pass null as the boundary. If no header is included in the message, pass null as the header and the parser will defaault to US-ASCII. The parseBody function returns a promise that will resolve with an object with the keys corresponding to the content type and decoded messages in human readable format. The following contrived example shows how to use mail-body-parser.
$3
`
--simpleboundary
content-type: text/plain
content-transfer-encoding: quoted-printable
Hello World=3F
--simpleboundary
content-type: text/html
content-transfer-encoding: quoted-printable
=3Chtml=3E=3Cp=3EHello World=3F=3C/p=3E=3Chtml=3E
--simpleboundary--
`
$3
`javascript
const { parseBody } = require("mail-body-parser");
const boundary = "simpleboundary";
const header = "content-type: multipart/alternative";
const message = "";
const getBodyParts = async () => {
const bodyParts = await parseBody(boundary, header, message);
return bodyParts;
}
console.log(getBodyParts());
`
The expected output is:
`javascript
{
"text": "Hello World?",
"html": "Hello World?
"
}
`
API
parseBody(<string> boundary, <string> header, <string> message) - Promise* - Returns the decoded text associated with the email message, or messages in the case of multipart/alternative. The returned promise is resolved with an object with the following properties:
text - string* - The decoded plain text message
html - string* - The decoded html message
Note: if the input message did not include a text/plain or text/html body, text or html properties will not be included in the object.
Issues and Feature Requests
Got a problem? Or a new idea you would like to see implemented? Either way, please open a new issue.
Tests
This project uses the Jest JavaScript Testing Framework. To run the tests, install Jest and its dependencies. After Jest is installed, run $ npm test` from the console.