How to send mail from node js

There are several ways to send mail from a Node.js application. Here are a few options:

  1. Nodemailer: Nodemailer is a popular and widely-used library for sending emails from Node.js. It supports various transport methods, including SMTP, Sendmail, and Amazon SES.

Install Nodemailer using npm:

npm install nodemailer

Here's an example of how to use Nodemailer:

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
  host: 'smtp.example.com',
  port: 587,
  secure: false, // or 'STARTTLS'
  auth: {
    user: 'username',
    pass: 'password'
  }
});

let mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Hello from Node.js',
  text: 'Hello from Node.js',
  html: '<b>Hello from Node.js</b>'
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error);
  }
  console.log('Email sent: ' + info.response);
});
  1. EmailJS: EmailJS is another popular library for sending emails from Node.js. It provides a simple and easy-to-use API for sending emails.

Install EmailJS using npm:

npm install emailjs

Here's an example of how to use EmailJS:

const emailjs = require('emailjs');

let server = emailjs.server({
  host: 'smtp.example.com',
  port: 587,
  user: 'username',
  password: 'password'
});

let message = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Hello from Node.js',
  text: 'Hello from Node.js',
  html: '<b>Hello from Node.js</b>'
};

server.send(message, (err, message) => {
  if (err) {
    console.log(err);
  } else {
    console.log('Email sent: ' + message);
  }
});
  1. SMTP: You can also use the built-in net module in Node.js to send emails using SMTP.

Here's an example of how to use the net module:

const net = require('net');

let client = new net.Socket();

client.connect(587, 'smtp.example.com', () => {
  client.write('HELO example.com\r\n');
  client.write('AUTH LOGIN\r\n');
  client.write('username\r\n');
  client.write('password\r\n');
  client.write('MAIL FROM: [email protected]\r\n');
  client.write('RCPT TO: [email protected]\r\n');
  client.write('DATA\r\n');
  client.write('Subject: Hello from Node.js\r\n');
  client.write('Hello from Node.js\r\n');
  client.write('.\r\n');
  client.end();
});

Note that this example uses the net module to establish an SMTP connection and send the email. This approach is more low-level and requires more manual configuration than using a library like Nodemailer or EmailJS.

  1. Amazon SES: If you're using Amazon Web Services (AWS), you can use Amazon Simple Email Service (SES) to send emails from your Node.js application.

Install the AWS SDK using npm:

npm install aws-sdk

Here's an example of how to use Amazon SES:

const AWS = require('aws-sdk');
const ses = new AWS.SES({ region: 'your-region' });

let params = {
  Source: '[email protected]',
  Destination: {
    ToAddresses: ['[email protected]'],
    CcAddresses: [],
    BccAddresses: []
  },
  Message: {
    Body: {
      Text: {
        Data: 'Hello from Node.js'
      },
      Html: {
        Data: '<b>Hello from Node.js</b>'
      }
    },
    Subject: {
      Data: 'Hello from Node.js'
    }
  }
};

ses.sendEmail(params, (err, data) => {
  if (err) {
    console.log(err);
  } else {
    console.log(data);
  }
});

Note that this example uses the AWS SDK to send an email using Amazon SES. You'll need to configure your AWS account and set up an SES identity to use this approach.