How to Send an Email Using Nodemailer
If you want to send an email using Nodemailer, follow these steps:
Step 1: Install Nodemailer
Begin by installing Nodemailer using npm:
1 | npm install nodemailer |
Step 2: Import Nodemailer
Next, import the Nodemailer module in your Node.js script or application:
1 | import nodemailer from 'nodemailer'; |
Step 3: Initialize the Transporter
Create a transporter object that will be used to send the email. Here’s an example:
1 | const transporter = nodemailer.createTransport({ |
⚠️ NOTE: Make sure to replace
'smtp_user'
and'smtp_pass'
with the actual credentials of your SMTP server.
Step 4: Create the Email Options
Now, create an options
object to specify the details of the email you want to send:
1 | const options = { |
Step 5: Send the Email
Finally, use the sendMail()
method on the transporter
object to send the email. Pass the options
and a callback function that will be executed when the email is sent:
1 | transporter.sendMail(options, (err, info) => { |
Alternatively, you can use the promise-based syntax:
1 | const info = await transporter.sendMail(options); |
Full Example Code
Here’s the full example code:
1 | import nodemailer from 'nodemailer'; |
That’s it! You now know how to send an email using Nodemailer.
Tags: Nodemailer, Email, SMTP, Node.js