Sending exceptions to mail in NodeJS

Mustafa Ozdemir
Analytics Vidhya
Published in
3 min readOct 19, 2020

During the process of writing and maintaining an application development, we face countless bugs and errors. It is relatively easy to solve these in development time. After the app is deployed, still we may face some unexpected exceptions. Somehow, we have to find a way to handle these exceptions.

There are many better ways for handling those exceptions(e.g. Sentry). We are going to use a shortcut which is not really recommended. We will learn how to send these exceptions to mail with help of NodeJS.

We are going to use a mail module called nodemailer. Nodemailer is the most popular module in NodeJS world for sending e-mail between servers. It makes sending email quite easy.

Let’s create two js files. First file will be called as mail_server.js. We are going to put all the configure code in this file to be able to send email. The other one will be called app.js. In this file, We will write a try catch block and use the mail_server.js as an internal module to send errors to the declared email address.

Creating files
Let’s start by creating a file in which we will define the content. Create a new file and name it mail_server.js. As I mentioned above we need a package called nodemailer. So let’s install the module by running the following command in the terminal.
npm install nodemailer

Now, open mail_server.js and import the nodemailer package into it. To do that we use the require command in NodeJS.
var nodemailer = require(‘nodemailer’);

Next step is to create a function that includes all configurations to send email. I create a function called mailfunc which passes a parameter called errormessage.

var nodemailer = require(‘nodemailer’);function mailfunc(errormessage){
var transporter = nodemailer.createTransport({
service: ‘gmail’,
auth: {
user: ‘youremail@gmail.com’,
pass: ‘yourpassword’
}
});
var mailOptions = {
from: ‘youremail@gmail.com’,
to: ‘someoneelsesmail@outlook.com’,
subject: ‘Error Message’,
text: errormessage,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log(‘Email sent: ‘ + info.response);
}
});
}
exports.mailfunc= mailfunc;

Gmail will be the mail server that we are going to use for this project. To send email using Gmail as a service from third-party apps like our NodeJS app, we have to enable ‘Allow less secure apps’ in Gmail. Click here to enable the ‘Allow less secure apps’.

There is one more thing to add to the file and this is export the function we have created in mail_server.js. for other programs to use.
exports.mailfunc= mailfunc;

It is time to do some coding for app.js and import the mail_server.js that we just exported for other programs to use like a npm module. All we need to do is require it as we did so for other modules.
const mail = require(‘./mail_server’);

Let’s add try catch block to our app.js to see actually It is working.

const mail = require(‘./mail_server’);try {
throw ‘myException’; // generates an exception
}
catch (e) {
/*email exception object to specified mail address*/
mail.mailfunc(e.toString());
console.log(e);
}

As you see, all we need to do is call mailfunc in catch blocks. That’s all!! From now on, we will receive our app errors by email that would show up in production.

Thank you for clicking.

--

--