@fixers/mail
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i @fixers/mail

Weekly Downloads

15

Version

1.3.0

License

MIT

Unpacked Size

18.8 kB

Total Files

12

Last publish

Collaborators

  • lucacicada