Send Mail Using Node.js and gmail in few Simple Steps

We all know how tedious the stuff gets when we wan’t to send mail from any automated system.

For example,

ping someone when CRON job at remote VM Executed *
Get failure logs of your server in mail*
etc

So, today we are going to take a look at how…


This content originally appeared on DEV Community and was authored by Sudarshan Sawandkar

We all know how tedious the stuff gets when we wan't to send mail from any automated system.

For example,

  • ping someone when CRON job at remote VM Executed *
  • Get failure logs of your server in mail* etc

So, today we are going to take a look at how can we send mail from nodejs using just a gmail account and third party library node-mailer

  • Setting up G-mail :-

For sending mail's through your gmail A/C you must allow the " less secure app" access to your google A/C which is done here
a
https://myaccount.google.com/lesssecureapps

Step 1 :- Setting up node-mailer

Install node-mailer using npm or yarn (which is one prefer let me know in comments). I will use npm here because, it comes with node installation

npm i node-mailer 

Step 2 :- Setting up node-mailer and some basic configuration

After installing node-mailer, we have to setup some config's. For sending mail, we are going to use Gmail as a mail service and create a mail transported object. Which internally handles the SMTP (Simple Mail Transfer Protocol) transactions.

const nodemailer = require("nodemailer");

module.exports = {
    sendMail: function () {
        let mailTransporter = nodemailer.createTransport({
            service: "gmail",
            auth: {
                user:"yourUserName@gmail.com",
                pass: "<SecretPassword",
            },
        });
    },
};

Step 3 :- create mail object

Now it's time to create actual mail payload. It will contain of recipient mail address and other stuff like subject and the string which we want to send with the mail. This is the basic payload we can alter the stuff with great flexibility

        let mailDetails = {
            from: "yourUserName@gmail.com",
            to: "favPerson@anyDomain.com",
            subject: "You Were Hacked 8)",
            text: "System failure !",
        };

Step 4 :- Send Mail

Now, just hit it. ( I am handling result with callback, you can also use Promise here)

  mailTransporter.sendMail(mailDetails, function (err, data) {
            if (err) {
                console.log("Error Occurs");
            } else {
                console.log("Email sent successfully");
            }
        });

That's It. Now, we can send email to anyone with any email address without setting up out own mail server or anything else

Final Thoughts

I hope you all learnt something new from this post. Let me know if anything else I have to write in my next one

Thanks for reading :)


This content originally appeared on DEV Community and was authored by Sudarshan Sawandkar


Print Share Comment Cite Upload Translate Updates
APA

Sudarshan Sawandkar | Sciencx (2021-04-27T16:37:08+00:00) Send Mail Using Node.js and gmail in few Simple Steps. Retrieved from https://www.scien.cx/2021/04/27/send-mail-using-node-js-and-gmail-in-few-simple-steps/

MLA
" » Send Mail Using Node.js and gmail in few Simple Steps." Sudarshan Sawandkar | Sciencx - Tuesday April 27, 2021, https://www.scien.cx/2021/04/27/send-mail-using-node-js-and-gmail-in-few-simple-steps/
HARVARD
Sudarshan Sawandkar | Sciencx Tuesday April 27, 2021 » Send Mail Using Node.js and gmail in few Simple Steps., viewed ,<https://www.scien.cx/2021/04/27/send-mail-using-node-js-and-gmail-in-few-simple-steps/>
VANCOUVER
Sudarshan Sawandkar | Sciencx - » Send Mail Using Node.js and gmail in few Simple Steps. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/27/send-mail-using-node-js-and-gmail-in-few-simple-steps/
CHICAGO
" » Send Mail Using Node.js and gmail in few Simple Steps." Sudarshan Sawandkar | Sciencx - Accessed . https://www.scien.cx/2021/04/27/send-mail-using-node-js-and-gmail-in-few-simple-steps/
IEEE
" » Send Mail Using Node.js and gmail in few Simple Steps." Sudarshan Sawandkar | Sciencx [Online]. Available: https://www.scien.cx/2021/04/27/send-mail-using-node-js-and-gmail-in-few-simple-steps/. [Accessed: ]
rf:citation
» Send Mail Using Node.js and gmail in few Simple Steps | Sudarshan Sawandkar | Sciencx | https://www.scien.cx/2021/04/27/send-mail-using-node-js-and-gmail-in-few-simple-steps/ |

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.