npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mail-compose

v1.0.0

Published

A simple library that composes MIME-encoded emails from human-friendly contents.

Readme

mail-compose

npm version

A simple library that composes MIME-encoded emails from human-friendly contents (like text, files, images), which can be fed into MTA/MSAs like sendmail, msmtp, ssmtp.

Basic Usage

Install the package:

npm install mail-compose

Create a mail object:

const Mail = require("mail-compose");
const fs = require("fs");

let mail = new Mail({
    from: {
        name: "Alice",
        address: "[email protected]"
    },
    to: {
        name: "Bob",
        address: "[email protected]"
    },
    subject: "Photo",
    message: "My new haircut!",
    attachments: [
        {
            filename: "new_look.jpg",
            content: fs.readFileSync("/home/alice/Desktop/new_look.jpg")
        }
    ]
});

Once the mail is created, you can call getMail() to get the composed mail, or getMailHeader(), getMailBody() if you want them to come separately.

console.log(mail.getMail());

Output:

From: "Alice" <[email protected]>
To: "Bob" <[email protected]>
Subject: Photo
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary-f81912011413b3a9b1a2fbbd1bd2600b"

--boundary-f81912011413b3a9b1a2fbbd1bd2600b
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

TXkgbmV3IGhhaXJjdXQh
--boundary-f81912011413b3a9b1a2fbbd1bd2600b
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="new_look.jpg"

/9j/4AAQSkZJRgABAQAASABIAAD/4QDORXhpZgAATU0AKgAAAAgABgESAAMAAAABAAEAAAEaAAU
AAAABAAAAVgEbAAUAAAABAAAAXg (... remaining base64-encoded data ...)
--boundary-f81912011413b3a9b1a2fbbd1bd2600b--

Parameters

The Mail class requires an object parameter:

{
    from: string|{name: string, address: string},
    to: string|{name: string, address: string},
    subject: string,
    message: string|{plainText: string, htmlText: string, assets: [string|{filename: string, content: Buffer}]}
    attachments: [string|{filename: string, content: Buffer}]
}

While UTF-8 is the de facto standard of modern days coding, this library accepts and only accepts UTF-8 parameters, whether it's the mail's subject or the name of sender/recipient.

from, to

These 2 parameters require either a string of mail address, or an object that includes a name, and an address.

let mail = new Mail({
    from: {
        name: "Alice", 
        address: "[email protected]"
    },
    to: "[email protected]",
    /* ... */
});

The address format defined by RFC 2822 should also work. However, you may want to stick with the name+address combination since the library will encode the name for you if any non-ASCII character presents.

let mail = new Mail({
    from: "Alice <[email protected]>",
    to: "Bob <[email protected]>",
    /* ... */
});

message

To get started quickly, drop a string here, as it will be parsed as text/plain content.

let mail = new Mail({
    /* ... */
    message: "Hello!",
    /* ... */
});

To send HTML contents, use:

let mail = new Mail({
    /* ... */
    message: {
        htmlText: "<p>Hello!</p>",
        plainText: "Hello!"
    },
    /* ... */
});

While using htmlText alone is possible, it's recommended to provide both plain texts along with HTML text, as it will be composed into a multipart/alternative, thus improving compatibilities/readabilities on some clients.

It's also possible to embed image assets into the mail's body.

let mail = new Mail({
    /* ... */
    message: {
        htmlText: "<p>My new haircut!</p><img src=\"new_look.jpg\">",
        plainText: "My new haircut!",
        assets: [
            {
                filename: "new_look.jpg",
                content: fs.readFileSync("/home/alice/Desktop/new_look.jpg")
            }
        ]
    },
    /* ... */
});

The message.assets array receives a list of file that shares the same format with attachments, which we'll cover later.

attachments

To compose mails with attachments, use:

let mail = new Mail({
    /* ... */
    attachments: [
        {
            filename: "transcations_q1.xlsx",
            content: fs.readFileSync("/path/transcations_q1.xlsx")
        },
        {
            filename: "transcations_q2.xlsx",
            content: fs.readFileSync("/path/transcations_q2.xlsx")
        }
    ]
});

To attach existing files more simply, you can also put their path directly into the array.

let mail = new Mail({
    /* ... */
    attachments: [
        "/path/transcations_q1.xlsx",
        "/path/transcations_q2.xlsx"
    ]
});

The name of these files will keep as is, however.