node-multi-mailer
A simple node.js module that exposes high-level API of Nodemailer and SendGrid.
Installation
Prerequisites
Install Package
The following recommended installation requires npm. If you are unfamiliar with npm, see the npm docs. Npm comes installed with Node.js since node version 0.8.x, therefore, you likely already have it.
npm install --save node-multi-mailer
yarn add node-multi-mailer
Verify Sender Identity from SendGrid
Verify an email address or domain in the Sender Authentication tab. Without this, you will receive a 403 Forbidden
response when trying to send mail.
Examples
If you want to use an email template, then add an email
folder in your project root directory and create a login.ejs
file in the email
folder. You can use any template name you want.
Add attachment
folder in your root directory for sending an email with attachments.
Folder structure with mail template:
+-- email
| +-- login.ejs
+-- attachments
| +-- myImage.png
| +-- resume.pdf
| +-- resume.docx
+-- node_modules
+-- index.js
+-- package.json
If required, use this login.ejs
file as an example template.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Node-Multi-Mailer</title>
</head>
<body>
<h1>Hey <%= firstName %> <%= lastName %>, Your OTP is <%= otp %></h1>
</body>
</html>
Implementation with NodeJs and SendGrid
const multiMailer = require("node-multi-mailer");
const multiMailer = multiMailer.configuration({
senderEmail: "you@example.com",
senderName: "your business name",
sendGridApiKey: SENDGRID_API_KEY,
replyTo: "me@example.com",
templateFolderPath: emailFolder,
attachmentFolderPath: attachmentFolder,
});
A normal plain text email without any attachments
var multiMailer = require("node-multi-mailer");
multiMailer.configuration({
senderEmail: "you@example.com",
senderName: "your business name",
sendGridApiKey: SENDGRID_API_KEY,
replyTo: "me@example.com",
});
var RECEIVER_EMAIL = "developer.shiv2020@gmail.com";
multiMailer.SendGrid.sendTextEmail(
RECEIVER_EMAIL,
"Sending email from node-multi-mailer",
"Thanks for sending email with node-multi-mailer"
);
A normal plain text email with attachments
var multiMailer = require("node-multi-mailer");
var path = require("path");
var attachmentFolder = path.join(__dirname, "attachment");
let attachments = ["myImage.png", "resume.pdf", "resume.docx"];
multiMailer.configuration({
senderEmail: "you@example.com",
senderName: "your business name",
sendGridApiKey: SENDGRID_API_KEY,
replyTo: "me@example.com",
templateFolderPath: emailFolder,
attachmentFolderPath: attachmentFolder,
});
var RECEIVER_EMAIL = "developer.shiv2020@gmail.com";
multiMailer.SendGrid.sendTextEmail(
RECEIVER_EMAIL,
"Sending email from node-multi-mailer",
"Thanks for sending email with node-multi-mailer"
attachments,
);
A template email using EJS without attachments
var multiMailer = require("node-multi-mailer");
var path = require("path");
var emailFolder = path.join(__dirname, "email");
multiMailer.configuration({
senderEmail: "test@example.com",
senderName: SENDER_NAME,
sendGridApiKey: SENDGRID_API_KEY,
replyTo: "test@example.com",
templateFolderPath: emailFolder,
});
var RECEIVER_EMAIL = "developer.shiv2020@gmail.com";
var data = {
fisrtName: "Shiv",
lastName: "Kumar",
otp: "123456",
};
multiMailer.SendGrid.sendEjsTemplateWithData(
RECEIVER_EMAIL,
"Sending email from node-multi-mailer",
"login.ejs",
data
);
A template email using EJS with attachments
var multiMailer = require("node-multi-mailer");
var path = require("path");
var attachmentFolder = path.join(__dirname, "attachment");
var emailFolder = path.join(__dirname, "email");
let attachments = ["myImage.png", "resume.pdf", "resume.docx"];
multiMailer.configuration({
senderEmail: "test@example.com",
senderName: SENDER_NAME,
sendGridApiKey: SENDGRID_API_KEY,
replyTo: "test@example.com",
templateFolderPath: emailFolder,
attachmentFolderPath: attachmentFolder,
});
var RECEIVER_EMAIL = "developer.shiv2020@gmail.com";
var data = {
fisrtName: "Shiv",
lastName: "Kumar",
otp: "123456",
};
multiMailer.SendGrid.sendEjsTemplateWithData(
RECEIVER_EMAIL,
"Sending email from node-multi-mailer",
"login.ejs",
data
attachments,
);
Implementation with ExpressJs
A template email using EJS without attachments
var express = require("express");
var multiMailer = require("node-multi-mailer");
var path = require("path");
var attachmentFolder = path.join(__dirname, "attachment");
var emailFolder = path.join(__dirname, "email");
let attachments = ["myImage.png", "resume.pdf", "resume.docx"];
multiMailer.configuration({
senderEmail: "test@example.com",
senderName: SENDER_NAME,
sendGridApiKey: SENDGRID_API_KEY,
replyTo: "test@example.com",
templateFolderPath: emailFolder,
attachmentFolderPath: attachmentFolder,
});
var app = express();
app.get("/", async (req, res) => {
var RECEIVER_EMAIL = "developer.shiv2020@gmail.com";
var data = {
fisrtName: "Shiv",
lastName: "Kumar",
otp: "123456",
};
await multiMailer.SendGrid.sendEjsTemplateWithData(
RECEIVER_EMAIL,
"Sending email from node-multi-mailer",
"login.ejs",
data
attachments,
);
return res.send("Email sent");
});
Contributors
License
MIT