Send mails using NodeJS
Tags: node, javascript, webdev, beginners
Published: 2021-07-02 08:43:10

Hey, fams! today we are going to learn how to send e-mails, right from our IDE using NodeJS. The module of interest is called Nodemailer.

![puppy](https://media.giphy.com/media/UwrdbvJz1CNck/giphy.gif)

## Prerequisites
🔗 NodeJs 
🔗 Nodemailer
🔗 Email account

---

🎯 Steps
Open editor (VSCode 😁), initialize your project with the command below
```JavaScript
npm init -y 
```
This command initiates a `package.json`, `package.json.lock`, and `index.js` (main entry file). The `index.js` will house all our logic.

## Dependencies 
📌Install Nodemailer
```JavaScript
npm i nodemailer
```
📌 Import the package inside `index.js`
```JavaScript
const nodemailer = require('nodemailer');
```
---

👨🏽‍🏫 For security reasons, make sure you install and use **dot.env** package to prevent your password from being exposed or pushed to GitHub.
`Install dotenv`
```JavaScript
npm i dotenv -S
```
![ignor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oxqlv82xhsdvo8qd9hy.JPG)
 
Require dotenv in your `index.js` file. I didn't require it in this project because I am using dummy data.
```JavaScript
require('dotenv').config();
```
Then, create a `.env` file your email and password
```JavaScript
Email= ***********@gmail.com
Password= ******
```
![env](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wojtfrfsmugotenhp6dw.JPG)

---
## Logic
🎯 Your auth logic in `index.js` with `dotenv`
```JavaScript
// Gmail account info
const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: process.env.EMAIL,
        pass: process.env.PASSWORD
    }
});
```

🎯 Your auth logic in `index.js` without `dotenv`. Write the logic below and of course change the email to your own and the password to yours too.

```JavaScript
// Gmail account info
const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'dsimple@gmail.com',
        pass: 'ilovemymama'
    }
});
```
---
🎯 Next use the mailOption to send your message.
```JavaScript
// Email info
const mailOptions = {
    from: 'dsimple@gmail.com',
    to: 'fams@gmail.com',
    subject: 'How to send emails using NodeJS',
    text: 'Follow the instructions and you will be fine'
};
```
---

🎯 Lastly, write: 
```JavaScript
// Send email 📧  and retrieve server response
transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});
```

When done properly, you should have the following logic in your `index.js`. That is if you choose not to use the `dotenv` 
![carbon (28)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4lcvg28ffo3hgnby1wl.png)

To run: type 👇🏼 in your terminal
```JavaScript
node index
```
---
Note: On your Gmail, do not forget to accept and allow the **"Less secure apps"** access to use your scripts with your Gmail SMTP connection. Gmail will alert you with an error if this option is off, you need to turn it on.
![alert](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8flcnjd5znq52zuv4tg.jpg)

[Disable Less App Here](https://myaccount.google.com/lesssecureapps)
 
## Multiple emails, CC and BCC
```JavaScript
const mailOptions = {
    from: 'dsimple@gmail.com',
    to: 'fams@gmail.com,myrealfams@gmail.com',
    cc: 'lexus@gmail.com',
    bcc: 'sugar@gmail.com',
    subject: 'How to send emails using NodeJS',
    text: 'Follow the instructions and you will be fine'
};
```
---
## Send attachment
```JavaScript
const mailOptions = {
    from: 'dsimple@gmail.com',
    to: 'fams@gmail.com,myrealfams@gmail.com',
    cc: 'lexus@gmail.com',
    bcc: 'sugar@gmail.com',
    subject: 'How to send emails using NodeJS',
    text: 'Follow the instructions and you will be fine',
    attachments: [{
    filename: "robocop.jpg", path: "./img/robocop.jpg"}]
};
```

Thanks 🙌🏽 for reading
[![Read my Web Socket](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrgtqfoxxg78kugbqhnm.png)](https://dev.to/drsimplegraffiti/chat-app-using-socket-io-1hp0)

{% github drsimplegraffiti/drsimplegraffiti %}

---
## Discuss
What other email 📬 services can you use apart from Gmail without toggling off the Less Secure App setting?

## Reference
[Download NodeJs](https://nodejs.org/en/)
[npm Reference](https://www.npmjs.com/package/nodemailer)
[Nodemailer site](https://nodemailer.com/about/)