Send mails using NodeJS

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.

Prerequisites

? NodeJs
? Nodemailer
? Email account

? Steps
Open editor (VSCode ?), initialize y…


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi

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

Prerequisites

? NodeJs
? Nodemailer
? Email account

? Steps
Open editor (VSCode ?), initialize your project with the command below

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

npm i nodemailer

? Import the package inside index.js

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

npm i dotenv -S

ignor

Require dotenv in your index.js file. I didn't require it in this project because I am using dummy data.

require('dotenv').config();

Then, create a .env file your email and password

Email= ***********@gmail.com
Password= ******

env

Logic

? Your auth logic in index.js with dotenv

// 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.

// Gmail account info
const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'dsimple@gmail.com',
        pass: 'ilovemymama'
    }
});

? Next use the mailOption to send your message.

// 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:

// 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)

To run: type ?? in your terminal

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

Disable Less App Here

Multiple emails, CC and BCC

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

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',
    attachment: [{
    filename: "robocop.jpg", path: "./img/robocop.jpg"}]
};

Thanks ?? for reading
Read my Web Socket

GitHub logo drsimplegraffiti / drsimplegraffiti

Config files for my GitHub profile.

Calm Developer

Hi ?, I'm Abayomi

A passionate Web developer from Nigeria

drsimplegraffiti

drsimplegraffiti

drsimplegraffi1

  • ? I’m currently working on Bookoroma

  • ? I’m currently learning Backend Node Js

Connect with me:

drsimplegraffiti drsimplegraffi1 15661401 drsimplegraffiti @drsimplegraffiti

Languages and Tools:

bootstrap css3 express git heroku html5 illustrator javascript mongodb nodejs photoshop postman react sass xd

Support:

drsimplegraffiti



drsimplegraffiti

 drsimplegraffiti

drsimplegraffiti

Discuss

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

Reference

Download NodeJs
npm Reference
Nodemailer site


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi


Print Share Comment Cite Upload Translate Updates
APA

Abayomi Ogunnusi | Sciencx (2021-07-02T08:43:10+00:00) Send mails using NodeJS. Retrieved from https://www.scien.cx/2021/07/02/send-mails-using-nodejs/

MLA
" » Send mails using NodeJS." Abayomi Ogunnusi | Sciencx - Friday July 2, 2021, https://www.scien.cx/2021/07/02/send-mails-using-nodejs/
HARVARD
Abayomi Ogunnusi | Sciencx Friday July 2, 2021 » Send mails using NodeJS., viewed ,<https://www.scien.cx/2021/07/02/send-mails-using-nodejs/>
VANCOUVER
Abayomi Ogunnusi | Sciencx - » Send mails using NodeJS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/02/send-mails-using-nodejs/
CHICAGO
" » Send mails using NodeJS." Abayomi Ogunnusi | Sciencx - Accessed . https://www.scien.cx/2021/07/02/send-mails-using-nodejs/
IEEE
" » Send mails using NodeJS." Abayomi Ogunnusi | Sciencx [Online]. Available: https://www.scien.cx/2021/07/02/send-mails-using-nodejs/. [Accessed: ]
rf:citation
» Send mails using NodeJS | Abayomi Ogunnusi | Sciencx | https://www.scien.cx/2021/07/02/send-mails-using-nodejs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.