DEV Community

Cover image for Integrating Nodemailer in a Node.js Project
Ashwani Singh
Ashwani Singh

Posted on

Integrating Nodemailer in a Node.js Project

Integrating Nodemailer in a Node.js Project

To integrate Nodemailer in a Node.js project, you can follow these steps:

1. Install Nodemailer

npm install nodemailer --save
Enter fullscreen mode Exit fullscreen mode

This will install Nodemailer and add it to your project's dependencies .

2. Set Up Nodemailer in Your Project:

After installing Nodemailer, you can include it in your Node.js application using the following code:

var nodemailer = require('nodemailer');
Enter fullscreen mode Exit fullscreen mode

If you are using ES modules, you can use the import statement instead .

3. Create a Transporter:

Use the createTransport method to create a transporter for sending emails. You can specify the email service (e.g. Gmail, outlook, aws ses ect.) and provide authentication details:

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

-------------------------or--------------------------
var transporter = nodemailer.createTransport({
        service: "Outlook365",
        host: "smtp.office365.com",
        port: "587",
        auth: {
         user: 'youremail@gmail.com',
         pass: 'yourpassword',
        },
        requireTLS: true,
      });

Enter fullscreen mode Exit fullscreen mode

Replace 'youremail@gmail.com' with your email address and 'yourpassword' with your email password .

4. Send an Email:

Once the transporter is set up, you can use it to send an email. Here's an example of how to send a simple email:

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'recipient@example.com',
  subject: 'Subject of the Email',
  text: 'Body/template of the Email'
};

transporter.sendMail(mailOptions, function(error, info) {
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Enter fullscreen mode Exit fullscreen mode

Replace 'youremail@gmail.com' with your email address, 'recipient@example.com' with the recipient's email address, and provide the subject and body of the email as needed .

These steps outline the process of integrating Nodemailer in a Node.js project and sending emails using Nodemailer and Gmail/outlook/aws ect. Remember to handle sensitive information such as email credentials securely in your project.

Conclusion

If you want to make you gmail/outlook will able to send email through code, you will also need to provide authentication of nodemailer in setting of gmail/aws/outlook etc.

For best practice, put all the credential in .env file, and call it in the function to make active.

e.g:

.env file

SMTP_PASS = xxxxxxxx
SMTP_HOST = amazonaws.com / gmail / office365.com
STMP_PORT = 5187
FROM_Email = youremail@gmail.com
Enter fullscreen mode Exit fullscreen mode

create an export function to call when send an mail.

export function mailSend(email_id, otp) {

    let mailTransporter = nodemailer.createTransport({
        service: 'aws',
        host: process.env.SMTP_HOST,
        auth: {
            user: process.env.SMTP_USER_NAME,
            pass: process.env.SMTP_PASS
        }
    });

    let mailDetails = {
        from: process.env.FROM_Email,
        to:   `${email_id}`,
        subject: 'Otp send by Team',
        text : `OTP is ${otp}`

    };

    mailTransporter.sendMail(mailDetails, function(err, data) {
        if(err) {
            console.log('Error Occurs', err);
        } else {
            console.log('Email sent successfully');
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

Call mailSend function when you want to initiate nodemailer to send otp.

mailSend(email_id, '983844');

Enter fullscreen mode Exit fullscreen mode

Top comments (0)