automated email sending using node js server
- Install the nodemailer package using npm by running the command:
npm install nodemailer
- Require the nodemailer module in your Node.js file:
const nodemailer = require('nodemailer');
- Create a transporter using nodemailer, providing the SMTP server details:
let transporter = nodemailer.createTransport({
host: 'your_smtp_server_host',
port: your_smtp_server_port,
secure: false, // true for 465, false for other ports
auth: {
user: 'your_email_address',
pass: 'your_email_password'
}
});
- Set up the email options including the sender, recipient, subject, and body:
let mailOptions = {
from: 'your_email_address',
to: 'recipient_email_address',
subject: 'Subject of your email',
text: 'Your email content goes here'
};
- Send the email using the transporter and mail options created:
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
- Handle any errors that may occur during the sending process.