How to send Email with node js for free

If you are building a project that involves customer interactions, then probably at some point you would have to send them an email as well.

For example, on successful form submits, you need to send a confirmation email. Or on every purchase, a receip…

If you are building a project that involves customer interactions, then probably at some point you would have to send them an email as well.

For example, on successful form submits, you need to send a confirmation email. Or on every purchase, a receipt or order details.

You could hook up some of the existing apis like send in blue, mail chimp etc, but you can do it for free in nodejs itself.

Node Mailer is a nodejs module, that makes it easy to send emails.

Here’s how you do it;

-> First, install Node Mailer

npm install nodemailer

-> Then require('nodemailer')

-> Create a transporter

Transporter is the object that is able to send the email. It contains data about the connection.
I’m using gmail to send emails and this is how the transporter looks for me:

const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: myemail@gmail.com,
pass: password
}
});

-> We also need an object containing the message to be sent

const mailOptions = {
from: 'The Idea project',
to: toAddress,
subject: 'My first Email!!!',
text: "This is my first email. I am so excited!"
};

You can send html emails with html key instead of text.

-> Next, to actually send the email, use
transporter.sendMail(mailOptions, callback)

The call back takes error and info arguments and is executed once the sending process is complete. You can use this to log errors if any.

You can customise the emails you send, where you send from and how you send it anyway you want. Read the docs here.

The complete code should look like this,

![Full code nodemailer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i116of2cgdf0d7eljgtb.png)

Print Share Comment Cite Upload Translate
APA
fawazsullia | Sciencx (2024-03-28T11:10:32+00:00) » How to send Email with node js for free. Retrieved from https://www.scien.cx/2021/05/31/how-to-send-email-with-node-js-for-free/.
MLA
" » How to send Email with node js for free." fawazsullia | Sciencx - Monday May 31, 2021, https://www.scien.cx/2021/05/31/how-to-send-email-with-node-js-for-free/
HARVARD
fawazsullia | Sciencx Monday May 31, 2021 » How to send Email with node js for free., viewed 2024-03-28T11:10:32+00:00,<https://www.scien.cx/2021/05/31/how-to-send-email-with-node-js-for-free/>
VANCOUVER
fawazsullia | Sciencx - » How to send Email with node js for free. [Internet]. [Accessed 2024-03-28T11:10:32+00:00]. Available from: https://www.scien.cx/2021/05/31/how-to-send-email-with-node-js-for-free/
CHICAGO
" » How to send Email with node js for free." fawazsullia | Sciencx - Accessed 2024-03-28T11:10:32+00:00. https://www.scien.cx/2021/05/31/how-to-send-email-with-node-js-for-free/
IEEE
" » How to send Email with node js for free." fawazsullia | Sciencx [Online]. Available: https://www.scien.cx/2021/05/31/how-to-send-email-with-node-js-for-free/. [Accessed: 2024-03-28T11:10:32+00:00]
rf:citation
» How to send Email with node js for free | fawazsullia | Sciencx | https://www.scien.cx/2021/05/31/how-to-send-email-with-node-js-for-free/ | 2024-03-28T11:10:32+00:00
https://github.com/addpipe/simple-recorderjs-demo