Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
mailbuild is a low level rfc2822 message composer. Define your own mime tree, no magic included.
mailbuild is a low level rfc2822 message composer. Define your own mime tree, no magic included.
This module requires TextEncoder
and TextDecoder
to exist as part of the StringEncoding API (see: MDN whatwg.org). Firefox 19+ is basically the only browser that supports this at the time of writing, while Chromium in canary, not stable. Luckily, there is a polyfill!
npm install --save mailbuild
This module has dependencies that will be fetched automatically.
Create a new Mailbuild
object with
var builder = new Mailbuild(contentType [, options]);
Where
filename
option if available)The same methods apply to the root node created with new Mailbuild()
and to any child nodes.
Creates and appends a child node to the node object
node.createChild(contentType, options)
The same arguments apply as with new Mailbuild()
. Created node object is returned.
Example
new Mailbuild("multipart/mixed").
createChild("multipart/related").
createChild("text/plain");
Generates the following mime tree:
multipart/mixed
↳ multipart/related
↳ text/plain
Appends an existing child node to the node object. Removes the node from an existing tree if needed.
node.appendChild(childNode)
Where
Method returns appended child node.
Example
var childNode = new Mailbuild("text/plain"),
rootNode = new Mailbuild("multipart/mixed");
rootnode.appendChild(childNode);
Generates the following mime tree:
multipart/mixed
↳ text/plain
Replaces current node with another node
node.replace(replacementNode)
Where
Method returns replacement node.
Example
var rootNode = new Mailbuild("multipart/mixed"),
childNode = rootNode.createChild("text/plain");
childNode.replace(new Mailbuild("text/html"));
Generates the following mime tree:
multipart/mixed
↳ text/html
Removes current node from the mime tree. Does not make a lot of sense for a root node.
node.remove();
Method returns removed node.
Example
var rootNode = new Mailbuild("multipart/mixed"),
childNode = rootNode.createChild("text/plain");
childNode.remove();
Generates the following mime tree:
multipart/mixed
Sets a header value. If the value for selected key exists, it is overwritten.
You can set multiple values as well by using [{key:"", value:""}]
or
{key: "value"}
structures as the first argument.
node.setHeader(key, value);
Where
Method returns current node.
Example
new Mailbuild("text/plain").
setHeader("content-disposition", "inline").
setHeader({
"content-transfer-encoding": "7bit"
}).
setHeader([
{key: "message-id", value: "abcde"}
]);
Generates the following header:
Content-type: text/plain
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Message-Id: <abcde>
Adds a header value. If the value for selected key exists, the value is appended as a new field and old one is not touched.
You can set multiple values as well by using [{key:"", value:""}]
or
{key: "value"}
structures as the first argument.
node.addHeader(key, value);
Where
Method returns current node.
Example
new Mailbuild("text/plain").
addHeader("X-Spam", "1").
setHeader({
"x-spam": "2"
}).
setHeader([
{key: "x-spam", value: "3"}
]);
Generates the following header:
Content-type: text/plain
X-Spam: 1
X-Spam: 2
X-Spam: 3
Retrieves the first mathcing value of a selected key
node.getHeader(key)
Where
Example
new Mailbuild("text/plain").getHeader("content-type"); // text/plain
Sets body content for current node. If the value is a string, charset is added automatically
to Content-Type (if it is text/*
). If the value is a Typed Array, you need to specify the charset yourself.
node.setContent(body)
Where
Example
new Mailbuild("text/plain").setContent("Hello world!");
Builds the rfc2822 message from the current node. If this is a root node, mandatory header fields are set if missing (Date, Message-Id, MIME-Version)
node.build()
Method returns the rfc2822 message as a string
Example
new Mailbuild("text/plain").setContent("Hello world!").build();
Returns the following string:
Content-type: text/plain
Date: <current datetime>
Message-Id: <generated value>
MIME-Version: 1.0
Hello world!
Generates a SMTP envelope object. Makes sense only for root node.
var envelope = node.generateEnvelope()
Method returns the envelope in the form of {from:'address', to: ['addresses']}
Example
new Mailbuild().
addHeader({
from: "From <from@example.com>",
to: "receiver1@example.com",
cc: "receiver2@example.com"
}).
getEnvelope();
Returns the following object:
{
"from": "from@example.com",
"to": ["receiver1@example.com", "receiver2@example.com"]
}
When setting address headers (From
, To
, Cc
, Bcc
) use of unicode is allowed. If needed
the addresses are converted to punycode automatically.
For attachments you should minimally set filename
option and Content-Disposition
header. If filename is specified, you can leave content type blank - if content type is not set, it is detected from the filename.
new Mailbuild("multipart/mixed").
createChild(false, {filename: "image.png"}).
setHeader("Content-Disposition", "attachment");
Obviously you might want to add Content-Id
header as well if you want to reference this attachment from the HTML content.
Most probably you only need to deal with the following multipart types when generating messages:
Examples
One content node and an attachment
multipart/mixed
↳ text/plain
↳ image/png
Content node with referenced attachment (eg. image with Content-Type
referenced by cid:
url in the HTML)
multipart/related
↳ text/html
↳ image/png
Plaintext and HTML alternatives
multipart/alternative
↳ text/html
↳ text/plain
One content node with referenced attachment and a regular attachment
multipart/mixed
↳ multipart/related
↳ text/plain
↳ image/png
↳ application/x-zip
Alternative content with referenced attachment for HTML and a regular attachment
multipart/mixed
↳ multipart/alternative
↳ text/plain
↳ multipart/related
↳ text/html
↳ image/png
↳ application/x-zip
git clone git@github.com:whiteout-io/mailbuild.git
cd mailbuild
npm install && npm test
grunt dev
go to http://localhost:12345/example/ to run the example
go to http://localhost:12345/test/ to run the tests in your browser of choice
Copyright (c) 2013 Andris Reinman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
FAQs
mailbuild is a low level rfc2822 message composer. Define your own mime tree, no magic included.
The npm package mailbuild receives a total of 31 weekly downloads. As such, mailbuild popularity was classified as not popular.
We found that mailbuild demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.