Send mail node js

In Node.js, you can send an email using various libraries and modules. Here are a few popular ones:

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

Here's an example of how to use Nodemailer:

const nodemailer = require('nodemailer');

// create a transporter object
let transporter = nodemailer.createTransport({
  host: 'smtp.example.com',
  port: 587,
  secure: false, // or 'STARTTLS'
  auth: {
    user: '[email protected]',
    pass: 'your-password'
  }
});

// define the email message
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>'
};

// send the email
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 in Node.js. It provides a simple and easy-to-use API for sending emails.

Here's an example of how to use EmailJS:

const emailjs = require('emailjs');

// create a server object
let server = emailjs.server({
  user: '[email protected]',
  password: 'your-password',
  host: 'smtp.example.com',
  port: 587
});

// define the email message
let message = {
  text: 'Hello from Node.js',
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Hello from Node.js'
};

// send the email
server.send(message, (error, message) => {
  if (error) {
    return console.log(error);
  }
  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');

// create a client object
let client = net.createConnection({
  host: 'smtp.example.com',
  port: 587
});

// define the email message
let message = 'Hello from Node.js';

// send the email
client.write('HELO example.com\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(message + '\r\n');
client.write('.\r\n');
client.end();

Note that you'll need to replace the placeholders ([email protected], your-password, smtp.example.com, etc.) with your actual email credentials and settings.