Sending emails in Node.js can be achieved using the popular NodeMailer library. NodeMailer is a module for Node.js that allows you to easily send emails using a variety of email transport services such as SMTP, sendmail, SES, and more. In this tutorial, we will walk through the steps to set up and use NodeMailer to send emails in Node.js.
Step 1: Install NodeMailer
First, you need to install NodeMailer as a dependency in your Node.js project. You can do this by running the following command in your terminal:
npm install nodemailer
Step 2: Require NodeMailer in your project
Next, you will need to require the nodemailer module in your Node.js file. You can do this by adding the following line of code at the beginning of your file:
const nodemailer = require('nodemailer');
Step 3: Set up a transporter
To send an email using NodeMailer, you will need to set up a transporter object that defines the email transport service you want to use (e.g. SMTP, sendmail, etc). Here’s an example of setting up a transporter object for using a SMTP server:
const transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'user@example.com',
pass: 'password'
}
});
Make sure to replace the placeholder values with your own SMTP server details.
Step 4: Create an email message
Now that you have set up the transporter, you can proceed to create an email message that you want to send. You can do this by defining an object that contains the email message details such as the sender, recipients, subject, and body of the email. Here’s an example of creating an email message object:
const mailOptions = {
from: 'user@example.com',
to: 'recipient@example.com',
subject: 'Hello from NodeMailer',
text: 'This is a test email sent from Node.js using NodeMailer.'
};
Step 5: Send the email
Finally, you can use the transporter object to send the email message using the sendMail
method. Here’s an example of sending the email message:
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
That’s it! You have now successfully sent an email using Node.js and NodeMailer. You can customize the email message further by adding HTML content, file attachments, and more using the options available in the mailOptions
object.
In this tutorial, we covered the basics of sending emails in Node.js using NodeMailer. For more advanced features and customization options, you can refer to the official NodeMailer documentation. Happy emailing!
Thank you. You are a great teacher. I hope you are blessed.
Thanks!!! Very helpful
Why do you need gmail as a host?
What is the function of the host?
Are there any open source hosts?
Thank you, sir, Your explanation is best. I understand a lot of new things
thank you
Sir, it’s work fine in dev, but not sending emails in production, what to do ?
emailjs plan offer 200 request for every month or just for one time 200 requests?
so easy when doing it locally. Once you try to deploy it that's when you get into issues
how to solve this error:
Error: self-signed certificate in certificate chain
at TLSSocket.onConnectSecure (node:_tls_wrap:1659:34)
at TLSSocket.emit (node:events:514:28)
at TLSSocket._finishInit (node:_tls_wrap:1070:8)
at ssl.onhandshakedone (node:_tls_wrap:856:12) {
code: 'ESOCKET',
command: 'CONN'
}
Thank you very much Sir
Nice video
Hey i have used your codes but for gmails it says queued.
can i do this in react also?
Thanks bro
is there any way to send large no emails to my customers?
Sir I'm working on latest Ubuntu system. I have an issue
/nodeMailer $ npm install nodemailer –save
up to date, audited 2 packages in 498ms
found 0 vulnerabilities
/nodeMailer $ ls
app.js node_modules package.json package-lock.json
/nodeMailer $ node app
TypeError: nodeMailer.createTransport is not a function
at main (/home/petty/WWW/nodeMailer/app.js:13:36)
at Object.<anonymous> (/home/petty/WWW/nodeMailer/app.js:36:1)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
node:internal/errors:484
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'nodeMailer' imported from /home/petty/WWW/nodeMailer/app.js
at new NodeError (node:internal/errors:393:5)
at packageResolve (node:internal/modules/esm/resolve:860:9)
at moduleResolve (node:internal/modules/esm/resolve:909:20)
at defaultResolve (node:internal/modules/esm/resolve:1124:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ESMLoader.import (node:internal/modules/esm/loader:525:22)
at importModuleDynamically (node:internal/modules/cjs/loader:1098:29)
at importModuleDynamicallyWrapper (node:internal/vm/module:438:21) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js v18.12.1
when deploy this program in c panel email not sent
Hello, how can I message you directly?
❤❤
Thank you sir!!
Hi is there any tutorial how to send attachment via file input?
How can i get my company's SMTP server hostname?? Plz help