Skip to content

Intelligent, minimal, server and client side mailer for NextJS, NodeJS and JS Applications and Servers.

Notifications You must be signed in to change notification settings

LarvenLLC/next-mailer

Repository files navigation

next-mailer

Intelligent, minimal, server and client side mailer for NextJS, NodeJS and JS Applications and Servers.

NPM JavaScript Style Guide

Install

npm install --save next-mailer

Or

yarn add next-mailer

Usage - Mailing

Configure env Variables

MAILER_BASE_URL env variable is mandatory. It represents the url at which the project runs. Some examples could be:

MAILER_BASE_URL=http://localhost:3000

Or

MAILER_BASE_URL=https://my-nextjs-website.com
  • Using one of known providers eg. gmail. See full list here.
MAILER_USER=account.email@example.com
MAILER_PASSWORD=smtp-password
MAILER_SERVICE=SendPulse
  • Using custom provider.
MAILER_USER=account.email@example.com
MAILER_PASSWORD=smtp-password
MAILER_HOST=smtp.hostname.com
MAILER_PORT=587

Other optional env variables:

MAILER_SENDER=Adam Beleko

** Other smtp mailer options can be passed while initializing NextMailer. See below.

1. Add API in /pages/api/mailer/index.js

  • Default initialization. Eg when using a known provider.
// /pages/api/mailer/index.js
import NextMailer from "next-mailer";

export default NextMailer();
  • Initialization with extra options
// /pages/api/mailer/index.js
import NextMailer from "next-mailer";

export default NextMailer({
  secureConnection: false,
  tls: {
      ciphers: 'SSLv3'
  }
});
  • Using custom logger
// /pages/api/mailer/index.js
import NextMailer from "next-mailer";

import log from "next-logs";

export default NextMailer({
  logger: {
    info: (message, object) => log.info(message, object)
    error: (message, object) => log.info(message, object)
  },
});

2. Client Side

The client side API uses API routes hence it works in both: client and server side.

// /pages/*.js
import mail from "next-mailer/react";

export default Page() {
  useEffect(() => {
    mail({
      receivers: 'bossbele@larven.io, mkoela@larven.io',
      sender: "Larven LLC",
      subject: 'Meeting',
      text: "Let's meet up at 10:00"
    });
    mail({
      receivers: 'bossbele@larven.io, mkoela@larven.io',
      sender: "Larven LLC",
      subject: 'Meeting',
      html: "<b>Let's meet up at 10:00</b>"
    });
  },[]);

  return (
    <div />
  )
};

3. Server Side

Next mailer ships with a server side API that makes mailing more efficient.

// /pages/api/auth.js || /middleware.js
import { mailer } from "next-mailer";

async function handler(req, res) {
  const { method } = req;

  try {
    switch (method) {
      case 'DELETE':
        // delete user
        mailer({
          receivers: 'user@larven.io',
          sender: "Noreply Larven",
          subject: 'Account Deletion',
          text: "Your account was deleted successfully"
        })
        break;
      default:
        res.setHeader('Allow', ['DELETE']);
        res.status(405).end(`Method ${method} Not Allowed`);
    }
    // send result
    return res.status(200).json({});
  } catch (error) {
    return res.status(500).json(error);
  }
}

NextJS Middleware

While using nextJS middleware in API routes, make sure that your middleware does not block requests at /api/mailer/ routes. This may lead to errors and malfunctioning while using next-mailer.

License

MIT © BossBele

About

Intelligent, minimal, server and client side mailer for NextJS, NodeJS and JS Applications and Servers.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published