Sending E-mails with NodeJs📧
Everyone sends and receives a number of emails daily. But did you ever think of sending emails with your Nodejs app?
Why don’t we send some today? We will use this amazing package named nodemailer to send emails. Let’s start by initializing our app!
I hope you have NodeJs installed on your PC, if not, you can do it here. Open a terminal in a folder and execute the following command to initiate the project.
npm init
Keep tapping “Enter” until all the questions are completed. Then execute this -
npm i dotenv ejs express nodemon nodemailer
Now, add files and folders in this pattern to your directory.
Now, we will be adding the start command in the package.json file for our server to run on nodemon something like this —
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
Now run the following command on your terminal to host the website on your local server. (Server)
npm start
So, now our server is all set!
Let’s start working over it… So, there are various npm packages available for sending emails. But, today we are going to use Nodemailer as I find it simple, quick, and handy.
Let’s quickly set up the routing function in index.js.
Let’s make an easy-to-go UI with bootswatch in the index.ejs of views folder.
Make sure you turn on the less secure apps of your Gmail account. Turning it on is not a very good practice with your account, thus, I prefer using a test email id!
Now, begin the Server and start sending emails to your loved ones. You can use nodemailer for contact forms or notifying people about some events… or anything that you wish!
I hope you will fall in love with Nodemailer and explore more. Feel free to ping me on LinkedIn in case you have any doubts!
Don’t forget to leave some claps if you liked this blog!