1. overview
Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It allows developers to build server-side and networking applications using JavaScript, which was traditionally used only for client-side scripting within web browsers.
Node.js is commonly used for building web servers and APIs, as well as for developing real-time applications like chat applications, streaming platforms, and online gaming servers. It has gained significant popularity among developers due to its performance, scalability, and the ability to use JavaScript for both client-side and server-side development, enabling full-stack JavaScript development.
2. Preparing
- install node js
- Have a google account
- Code editor (Recommend Visual Studio)
3. Create an SMTP
Go to Your Google Account => Manage Your Google Account => Security => 2 steps verification
Select App Password Section.
Enter app name and click to Create => Copy password and click to DONE
4. create a node express app
To test send email in node js, you should create a simple express app, You can read more about how to create simple express js app on link
– After creating express js app, You can create new router to test send email function. Open file index.js and add code
app.post('/sendemail', async(req, res) => {
res.status(200).json({status: 'ok'});
})
Test it in http request ( You can use Postman) to make sure that the router is working.
Well, Router is working, So we can add function send email for now.
– Install nodemailer
npm install nodemailer
import nodemailer to file index.js
// code
var nodemailer = require("nodemailer");
// code
Go to send email router and create Transport
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // Use `true` for port 465, `false` for all other ports
auth: {
user: "[email protected]",
pass: "yoursmtppass",
},
});
- Host: we using Gmail SMTP
- Port: 587 or 456
- user: your gmail account.
- pass: your Smtp password that you create in the step 3
Next: Create and send email option
const mailOption = {
from: '"Test Email Company" <[email protected]>', // sender address
to: "[email protected], [email protected]", // list of receivers
subject: "Hello Friend✔", // Subject line
text: "How are you", // plain text body
html: "<b>How are you? I am very miss you</b>", // html body
}
Finally. Call function send email, return message successfully if send email success.
try {
const isSend = await transporter.sendMail(mailOption);
console.log("Message sent: %s", isSend.messageId);
return res.status(200).send('send email success')
} catch (error) {
}
res.status(500).send('something wrong when sending email')
Back to check in the postman, You can see result:
Check in email, You can see email is send to friend.
Conclusion: Here is simple example about send email in node js. You can use it to send email in your web application using node js. If you have any question, You can comment in this post, We can discus further.