This code is an example of a Node.js module that uses the Nodemailer library to send emails. It defines an appMail function that takes an options object as a parameter and sends an email based on the provided options.
1. Import necessary modules:
import nodemailer from "nodemailer"; import hbs from "nodemailer-express-handlebars"; import path from "path";
The code imports the nodemailer, nodemailer-express-handlebars, and path modules using ES6 module syntax.
2. Set up Handlebars options for email templates:
const handlebarOptions = { viewEngine: { partialsDir: path.resolve('./views/'), defaultLayout: false, }, viewPath: path.resolve('./views/'), };
This code defines the options for using Handlebars as the template engine for email templates. It specifies the directory where the template files are located (./views/) and sets defaultLayout to false to indicate that there is no default layout for the templates.
3. Define the appMail function:
const appMail = async (options) => { // Code for sending the email };
The appMail function is an asynchronous function that takes an options object as a parameter. It will be used to configure and send the email.
4. Set up the email transporter:
const transporter = nodemailer.createTransport({ host: 'smtp.sendgrid.net', port: process.env.SENDGRID_PORT, auth: { user: process.env.SENDGRID_USERNAME, pass: process.env.SENDGRID_PASSWORD } });
This code creates an email transporter using Nodemailer’s createTransport method. It configures the transporter to use the SendGrid SMTP server (smtp.sendgrid.net). The port is obtained from the SENDGRID_PORT environment variable, and the username and password for authentication are obtained from the SENDGRID_USERNAME and SENDGRID_PASSWORD environment variables, respectively.
5. Define the email options:
const mailOptions = { from: `${options.from} via <stocknotify@prpwebs.com>`, to: options.email, subject: options.subject, template: 'appMails', context: { bodyTxt: options?.body } };
The mailOptions object defines various options for the email, including the sender (from), recipient (to), subject, and template to be used (appMails). It also specifies the context data that will be passed to the email template for rendering.
6. Set up Handlebars for email template compilation:
transporter.use('compile', hbs(handlebarOptions));
This code configures Nodemailer to use Handlebars as the template engine and compiles the email template with the provided Handlebars options.
7. Send the email:
return await transporter.sendMail(mailOptions);
The sendMail method is called on the transporter object, passing in the mailOptions object. The function returns a Promise that resolves to the result of sending the email.
Full Code:
import nodemailer from "nodemailer"; import hbs from "nodemailer-express-handlebars"; import path from "path"; const appMail = async (options) => { // Configure Handlebars options for email templates const handlebarOptions = { viewEngine: { partialsDir: path.resolve('./views/'), defaultLayout: false, }, viewPath: path.resolve('./views/'), }; // Create a transporter using SendGrid SMTP server const transporter = nodemailer.createTransport({ host: 'smtp.sendgrid.net', port: process.env.SENDGRID_PORT, auth: { user: process.env.SENDGRID_USERNAME, pass: process.env.SENDGRID_PASSWORD } }); // Define the email options const mailOptions = { from: `${options.from} via <stocknotify@prpwebs.com>`, to: options.email, subject: options.subject, template: 'appMails', context: { bodyTxt: options?.body } }; // Configure Nodemailer to use Handlebars for template compilation transporter.use('compile', hbs(handlebarOptions)); // Send the email using the transporter and return the result return await transporter.sendMail(mailOptions); }; export default appMail;
In this code, the appMail function includes the entire logic for configuring the email transporter, defining the email options, setting up Handlebars for template compilation, and sending the email using the transporter’s sendMail method.