Skip to content

Node mailer (Tejo)

TejoBurgboy edited this page Apr 6, 2022 · 29 revisions

Nodemailer.

image


What is Nodemailer

Node-mailer is a node module that can send e-mails to the user. Node-mailer is the most commonly used node module when it comes to sending e-mails. Node-mailer can be used for multiple things like: Email conformation, password changing and register confirmation. Node-Mailer also falls under the Mit licence.


Usage

Before you start writing code in your js you first need to install and require nodemailer. For this you need to do Npm install nodemailer then you need to require it by typing const nodemailer = require('nodemailer'); in your .js file you want to use it.
Node-Mailer has 3 staps: Transport setup, mailoptions and sendmail.

Transport setup

In het transport setup fase you set up the mail account from which the mail will be sent. There are multiple services you can sent the mail from Gmail, SMTP, sendmail, SES and stream. SMTP and Gmail are the most used ones. Gmail is the most easiest to use it also comes with a catch. Your application is most likely a less secured application according to google and Gmail does not accept mails from less secure applications. Luckily there is a sidebar in which you can choose to enable mails from less secure apps. Besides the service you also need to do the auth. Which exist of your email address and password you want to use. Don't forget to put those in your .env file. Under is a example way to do the transport setup in node.js.

                            let transporter = nodemailer.createTransport({
                                service: 'gmail',
                                auth: {
                                    user: mailuser, 
                                    pass: mailpass, 
                                },
                                tls: {
                                    rejectUnautorized: false
                                },
                            });

Mailoptions

In this one youโ€™re going to fill in what is going to come in the mail you want to sent to the user. Like the title and content. Also you need to define to who the mail is going to be send to.

                            let mailoptions = {
                                from: 'Cmd-Online',
                                to: email,
                                subject: 'Cmd-Online',
                                text: 'Regegisteert',
                                html: '<b> Uw registratie bij de cmd-online aplicatie is gelukt <b>',

                            };

Sendmail

The last step is the sendmail. This one is the most easy of them all. You just need to make a function in which youโ€™re going to use the transporter and send them with the mailoptions. After that you need to do some basic error handling and finish it with redirecting to the login page.

                            transporter.sendMail(mailoptions, (error, info) => {
                                if (error) {
                                    return console.log(error),
                                    res.json({
                                        error: error,
                                        message: 'er gaat hier iets fout',
                                    });
                                }
                                res.redirect('/users/login');
                            });

Node mailer for mail verification.

The way I explained node mailer was in a way where you only can send a confirmation mail to the user. But you can also send a e-mail verification mail to the user. If you want to send to a verification e-mail to the user you need to give the user a status of inactive while logging in. Than you need to make a unique url for the user and put it in the mail. Then the user clicks the links it makes his account status to active. I did not decided to use this because it may ruin the entire registration process.


Nodemailer review

I want to go quickly over my experience with nodemailer with the strong and the weak points of it.

Strong points

  • Very versatile in usage.
  • Easy to learn.

Weak points

  • Transport by sttp can be quite confusing.
  • Transport by google can be quite annoying because of the low sequirity aplication rule.

Sources