Intelligent, minimal, server and client side mailer for NextJS, NodeJS and JS Applications and Servers.
npm install next-mailer> Intelligent, minimal, server and client side mailer for NextJS, NodeJS and JS Applications and Servers.
 
``bash`
npm install --save next-mailer
Or
`bash`
yarn add next-mailer
env variable is mandatory. It represents the url at which the project runs.
Some examples could be:
`env
MAILER_BASE_URL=http://localhost:3000
`
Or
`env
MAILER_BASE_URL=https://my-nextjs-website.com
`- Using one of known providers eg. gmail. See full list here.
`env
MAILER_USER=account.email@example.com
MAILER_PASSWORD=smtp-password
MAILER_SERVICE=SendPulse
`- Using custom provider.
`env
MAILER_USER=account.email@example.com
MAILER_PASSWORD=smtp-password
MAILER_HOST=smtp.hostname.com
MAILER_PORT=587
`Other optional env variables:
`env
MAILER_SENDER=Adam Beleko
`** Other smtp mailer options can be passed while initializing NextMailer. See below.
$3
- Default initialization. Eg when using a known provider.`jsx
// /pages/api/mailer/index.js
import NextMailer from "next-mailer";export default NextMailer();
`- Initialization with extra options
`jsx
// /pages/api/mailer/index.js
import NextMailer from "next-mailer";export default NextMailer({
secureConnection: false,
tls: {
ciphers: 'SSLv3'
}
});
`- Using custom logger
`jsx
// /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)
},
});
`$3
The client side API uses API routes hence it works in both: client and server side.`jsx
// /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: "Let's meet up at 10:00"
});
},[]);
return (
)
};
`$3
Next mailer ships with a server side API that makes mailing more efficient.`jsx
// /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`.MIT © BossBele