Coder Social home page Coder Social logo

friendly-mail's Introduction

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()

friendly-mail's People

Contributors

bahdcoder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.