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 🙏

© 2024 – Pkg Stats / Ryan Hefner

friendly-mail

v1.0.3

Published

Mail provider for elegantly sending emails in node js.

Downloads

2

Readme

Friendly Mail 📩

Build Status

Elegant mail sender for node js.

Friendly Mail is simple, clean, and modern and easy to use email sending package for Nodejs built on top of nodemailer and uses driver implementations from Adonis Mail.

Supported mail drivers: smtp, mailgun, amazon-ses, sparkpost, ethereal

Installation

You can install the package using npm or yarn

npm install --save friendly-mail
# Using yarn
yarn add friendly-mail

Create a mail configuration file

To configure what drivers you'll be using to send emails, view engines and more, you need to generate a mail.config.js file in your project's root.

# Using npm
npx friendlymail init

# Using yarn
yarn friendlymail init

Setting it up

Here's an example of the configuration:

module.exports = {
    /*
    |--------------------------------------------------------------------------
    | Connection
    |--------------------------------------------------------------------------
    |
    | Connection to be used for sending emails. Each connection needs to
    | define a driver too.
    |
    */
    connection: process.env.MAIL_CONNECTION || 'smtp',

    /*
    |--------------------------------------------------------------------------
    | Views
    |--------------------------------------------------------------------------
    |
    | This configuration defines the folder in which all emails are stored.
    | If it is not defined, /mails is used as default.
    |
    */
    views: '/mails',

    /*
    |--------------------------------------------------------------------------
    | View engine
    |--------------------------------------------------------------------------
    |
    | This is the view engine that should be used. The currently supported are:
    | handlebars, edge
    |
    */
    viewEngine: 'handlebars',

    /*
    |--------------------------------------------------------------------------
    | SMTP
    |--------------------------------------------------------------------------
    |
    | Here we define configuration for sending emails via SMTP.
    |
    */
    smtp: {
        driver: 'smtp',
            pool: true,
            port: process.env.SMTP_PORT || 2525,
            host: process.env.SMTP_HOST || 'smtp.mailtrap.io',
            secure: false,
            auth: {
            user: process.env.MAIL_USERNAME,
            pass: process.env.MAIL_PASSWORD
        },
        maxConnections: 5,
        maxMessages: 100,
        rateLimit: 10
    },

    /*
    |--------------------------------------------------------------------------
    | SparkPost
    |--------------------------------------------------------------------------
    |
    | Here we define configuration for spark post. Extra options can be defined
    | inside the `extra` object.
    |
    | https://developer.sparkpost.com/api/transmissions.html#header-options-attributes
    |
    | extras: {
    |   campaign_id: 'sparkpost campaign id',
    |   options: { // sparkpost options }
    | }
    |
    */
    sparkpost: {
        driver: 'sparkpost',
        // endpoint: 'https://api.eu.sparkpost.com/api/v1',
        apiKey: process.env.SPARKPOST_API_KEY,
        extras: {}
    },

    /*
    |--------------------------------------------------------------------------
    | Mailgun
    |--------------------------------------------------------------------------
    |
    | Here we define configuration for mailgun. Extra options can be defined
    | inside the `extra` object.
    |
    | https://mailgun-documentation.readthedocs.io/en/latest/api-sending.html#sending
    |
    | extras: {
    |   'o:tag': '',
    |   'o:campaign': '',,
    |   . . .
    | }
    |
    */
    mailgun: {
        driver: 'mailgun',
        domain: process.env.MAILGUN_DOMAIN,
        apiKey: process.env.MAILGUN_API_KEY,
        extras: {}
    },

    /*
    |--------------------------------------------------------------------------
    | Ethereal
    |--------------------------------------------------------------------------
    |
    | Ethereal driver to quickly test emails in your browser. A disposable
    | account is created automatically for you.
    |
    | https://ethereal.email
    |
    */
    ethereal: {
        driver: 'ethereal'
    }
}

The mail.config.js file exports an object. The following configuration variables are required:

  • connection: This represents the name of the driver to use.
  • views: This is the folder in which all your emails are stored. It defaults to /mails
  • viewsEngine: This defines what templating engine you are using for emails. For now, only handlebars and edge are supported

The last configuration required is a configuration object specific to the driver. Here's an example configuration for smtp:

    smtp: {
        driver: 'smtp',
        pool: true,
        port: process.env.SMTP_PORT || 2525,
        host: process.env.SMTP_HOST || 'smtp.mailtrap.io',
        secure: false,
        auth: {
            user: process.env.MAIL_USERNAME,
            pass: process.env.MAIL_PASSWORD
        },
        maxConnections: 5,
        maxMessages: 100,
        rateLimit: 10
    },

Usage

Here's a sample piece of code to send an email:

const Mail = require('friendly-mail')

const nameOfEmail = 'confirm-email'

const recipientName = 'John Doe'
const recipientEmail = '[email protected]'

const subject = 'Please confirm your email address.'

// Send the mail using async/await
await new Mail(nameOfEmail)
    .to(recipientEmail, recipientName)
    .subject(subject)
    .send()

Note: All publicly exposed methods on the Mail class are chainable, except the send and sendRaw which return Promises.

Common use cases

Generating emails

The package ships with a command to generate help you scaffold emails.

# Using npm
npx friendlymail generate activate-account
# Using yarn
yarn friendlymail generate activate-account

Passing data to templates

The data method can be used to set data that will be passed to the email template.

await new Mail(nameOfEmail)
    .to(recipientEmail, recipientName)
    .subject(subject)
    .data({
        name: 'John Doe',
        url: 'https://google.com'
    })
    .send()

Setting cc and bcc for a mail

await new Mail(nameOfEmail)
    .inReplyTo('[email protected]', 'Jane Doe')
    .to(recipientEmail, recipientName)
    .subject(subject)
    .cc('[email protected]', 'Eren Stales')
    .bcc('[email protected]', 'Steve Dickson')
    .send()

Sending emails to multiple recipients

The to method can recieve an array of address objects to send emails to multiple users. This also works for all other methods that set user addresses like from cc bcc inReplyTo replyTo and sender

await new Mail(nameOfEmail)
    .inReplyTo([{ address: '[email protected]', email: 'Jane Doe' }])
    .to([{ address: '[email protected]', name: 'Foo' }])
    .subject('Monthly Newsletter')
    .cc([{ address: '[email protected]', name: 'Eren Stales' }])
    .bcc([{ address: '[email protected]', name: 'Steve Dickson' }])
    .send()

Sending mails with attachments

The attach and attachData methods can be used to send attachments

// Attaching an existing file

await new Mail(nameOfEmail)
    .to(recipientEmail, recipientName)
    .subject(subject)
    .attach('/absolute/path/to/file')
    .send()

// Attaching buffer as attachment with a custom file name
const filename = 'hello.txt'
const rawData = new Buffer('hello')
await new Mail(nameOfEmail)
    .to(recipientEmail, recipientName)
    .subject(subject)
    .attachData(rawData, filename)
    .send()

// Attaching readstream as attachment with a custom file name
const filename = 'hello.txt'
const rawData = fs.createReadStream('hello.txt')

await new Mail(nameOfEmail)
    .to(recipientEmail, recipientName)
    .subject(subject)
    .attachData(rawData, filename)
    .send()

// Attaching string as attachment with a custom file name
const filename = 'hello.txt'
const rawData = 'hello'

await new Mail(nameOfEmail)
    .to(recipientEmail, recipientName)
    .subject(subject)
    .attachData(rawData, filename)
    .send()