- Published on
Sending emails using nodemailer
- Authors
- Name
- John Mwendwa
- Github
- John
Are you struggling to find an easy way to send emails to clients or users of your website and you're limited budgetwise? Well here is an easy step by step on how you can incorporate email feature in your project without incurring any extra cost.
Step 1: Installing the library
Start by installing the nodemailer
library from the command line
1npm i nodemailer
Step 2 : Create file
Create a separate file where we are going to store our function calls i.e send_emails.tsx
Step 3: Importing the library
1import nodemailer from "nodemailer"
Step 4 : Defining the transporter props
First lets define the email props we'll be expecting.
1export interface EmailProps { 2 to: string | string[]; 3 cc?: string | string[]; 4 bcc?: string | string[]; 5 subject: string; 6 html?: string; 7 text?: string; 8}
Then lets define the email transport function properties. I have decided to keep the host prop dynamic since email hosts keep changing, because you may opt to start with gmail and then later change to a custom email.
1const transporter = nodemailer.createTransport({ 2 host: process.env.EMAIL_HOST, 3 port: parseInt(process.env.EMAIL_PORT || "465"), 4 secure: true, 5 auth: { 6 user: process.env.EMAIL_NAME, 7 pass: process.env.EMAIL_PASSWORD, 8 }, 9});
Step 5: Creating a reusable function
Lastly, we're going to create a reusable function that we'll be calling whenever we want to send an email/emails to users.
1export const sendEmail = async (mailOptions: EmailProps) => { 2 const res = await transporter.sendMail({ 3 ...mailOptions, 4 from: `John Mwendwa <${process.env.EMAIL_NAME}>`, 5 }); 6 return res; 7};
And you're basically done with the hard part. Now you can use the function in any part of your application by simply calling it like shown below
1//email to a single user 2 await sendEmail({ 3 to: "user_email", 4 subject: "Email verification", 5 text:"Please click the link below to verify your email" , 6 }); 7 8//emails to multiple users 9await sendEmail({ 10 to: ["user1","user2","user3","user4"], 11 subject: "Party Notification", 12 text:"This friday we'll be having a party to celebrate the achievements we've made so far. Ensure you make the necessary plans to be there." , 13 });
As you can see, for single email recipients, we use a single string email, and for multiple recipients, we use an array of comma separated emails"
For the whole code :
1import nodemailer from "nodemailer" 2 3export interface EmailProps { 4 to: string | string[]; 5 cc?: string | string[]; 6 bcc?: string | string[]; 7 subject: string; 8 html?: string; 9 text?: string; 10} 11 12const transporter = nodemailer.createTransport({ 13 host: process.env.EMAIL_HOST, 14 port: parseInt(process.env.EMAIL_PORT || "465"), 15 secure: true, 16 auth: { 17 user: process.env.EMAIL_NAME, 18 pass: process.env.EMAIL_PASSWORD, 19 }, 20}); 21 22export const sendEmail = async (mailOptions: EmailProps) => { 23 const res = await transporter.sendMail({ 24 ...mailOptions, 25 from: `John Mwendwa <${process.env.EMAIL_NAME}>`, 26 }); 27 return res; 28}; 29 30/*sending the email(s) */ 31 32//email to a single user 33 await sendEmail({ 34 to: "user_email", 35 subject: "Email verification", 36 text:"Please click the link below to verify your email" , 37 }); 38 39//emails to multiple users 40await sendEmail({ 41 to: ["user1","user2","user3","user4"], 42 subject: "Party Notification", 43 text:"This friday we'll be having a party to celebrate the achievements we've made so far. Ensure you make the necessary plans to be there." 44 }); 45 46 47
Conclusion
Nodemailer is a free/cheap alternative to use when you need to send emails to clients/users and you're working within a limited budget.