Git Product home page Git Product logo

mail's Introduction

Node mailer

npm version npm downloads License

A tiny wrapper around nodemailer, no dependencies!

Who am I kiddin, nodemailer is the dependency!

Starting from v1.3, only Node18 and above are supported

This package is ESM only!

Install

pnpm i @fixers/mail
npm install @fixers/mail

Usage

Make sure to add any optional dependency!

pnpm add @aws-sdk/client-ses
pnpm add nodemailer-mailgun-transport
import { createMailer } from '@fixers/mail'
import { mailgun } from '@fixers/mail/mailers/mailgun'
import { ses } from '@fixers/mail/mailers/ses'

const mailer = createMailer({
  default: 'mailgun',
  mailers: {
    mailgun: mailgun({
      domain: process.env.MAILGUN_DOMAIN,
      secret: process.env.MAILGUN_API_KEY
    }),
    ses: ses()
  }
})

// it uses 'mailgun', the default mailer
await mailer.sendMail({ })

// with 'ses'
await mailer.sendMailWith('ses', { })

Configure using nodemailer

If you prefer to configure nodemailer by yourself:

import { createMailer } from '@fixers/mail'
import { createTransport } from 'nodemailer'
import mg from 'nodemailer-mailgun-transport'

const mailer = createMailer({
  default: 'mailgun',
  mailers: {
    mailtrap: createTransport({
      host: process.env.MAIL_HOST,
      port: process.env.MAIL_PORT,
      auth: {
        user: process.env.MAIL_USERNAME,
        pass: process.env.MAIL_PASSWORD,
      },
    }),
    mailgun: createTransport(mg({
      auth: {
        domain: env.MAILGUN_DOMAIN,
        apiKey: env.MAILGUN_SECRET
      }
    }))
  }
})

With custom mailers

You can also use any mailer-compatible type:

pnpm add @types/nodemailer -D
import { createMailer } from '@fixers/mail'
import type { SendMailOptions } from 'nodemailer'

const mailer = createMailer({
  default: 'custom',
  mailers: {
    custom: {
      async sendMail(mailOptions: SendMailOptions) {
        // send an email home!
      }
    },
    // it can be a factory also, promise or not is up to you
    customFactory: async () => {
      return {
        async sendMail(mailOptions: SendMailOptions) {
          // send an email home!
        }
      }
    }
  }
})

Caveats

  • The mailer default is mandatory even when only a single mailer is defined, this might change in future

  • Typings are loose, you might need to install @types/nodemailer to get proper sendMail typings, this may change in future as this library could provide some minimal typings that matches nodemailer.

  • @aws-sdk/client-ses and nodemailer-mailgun-transport are optional peer dependency, you need to install them separately.

mail's People

Contributors

lucacicada avatar

Stargazers

Vittorio Gioda 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.