HomeBack-End & DatabaseSending Mails in Express- Nodemailer & Handlebars

Sending Mails in Express- Nodemailer & Handlebars

In this blog post we see how to use nodemailer library to send out emails from your express project. We will also see how to integrate handlebar to organize our email template and make it easier to send out emails.

Libraries used in this blog.
Nodemail library can be found at http://www.nodemailer.com/

Handlebars library can be found at http://handlebarsjs.com/ and https://www.npmjs.com/package/nodemailer-express-handlebars

In this blog post, i am assuming you are already familiar with expressjs and handlbars.

Sending Out Mails using NodeJS

So first to install libraries
[js]
$ npm install nodemailer –save
[/js]

To send mail from nodemailer, you need to create 2 things
1. Transport : This defines the actual transport being used like SMTP, GMail, Amazon SES, SendGrid etc
2. Mailer : Properties of the email like from,to,content, attachments etc

I will use the basic direct transport for now to demonstrate email sending


$ npm install nodemailer-direct-transport --save

[js]
var directTransport = require(‘nodemailer-direct-transport’);
var nodemailer = require(‘nodemailer’);
var options = {};
var transporter = nodemailer.createTransport(directTransport(options))
[/js]

We can also use gmail as transport easily
[js]
var nodemailer = require(‘nodemailer’);
var transporter = nodemailer.createTransport({
service: ‘gmail’,
auth: {
user: ‘[email protected]’,
pass: ‘password’
}
});
[/js]

When using gmail, make sure gmail doesn’t block your mail request. Gmail these has got very intelligent at blocking suspicious login and blocks such requests.

there are many more transport methods available http://www.nodemailer.com/#available-transports

Next to send our email simply do

[js]
transporter.sendMail({
from: ‘sender@address’,
to: ‘receiver@address’,
subject: ‘hello’,
html: ‘hello world!’
});
[/js]

Again there are many options for sending out emails which can be configured, mentioned here http://www.nodemailer.com/#sending-mail

Using the code above, you can send out a simple email from nodejs.

Transactional Mails using NodeMailer with Express and Handlebars

When sending out transactional mails from your website, there are few things you need.
1. Modularizing Your Template: If your sending out 10-20 different emails from you website you would want things like the header, footer,etc to be in a common template files.
2. Pass Variables To Template: Its important to able to pass variables to the email template.

For both these purposes we will integrate handlebars with nodemailer. In a previous post i have already discussed how to integrate handlebar with expressjs.

We will be using plugin at https://github.com/yads/nodemailer-express-handlebars for this purpose.

Here is a code to send out email using handlebars

[js]

var nodemailer = require(‘nodemailer’);
var hbs = require(‘nodemailer-express-handlebars’);
var options = {
viewEngine: {
extname: ‘.hbs’,
layoutsDir: ‘views/email/’,
defaultLayout : ‘template’,
partialsDir : ‘views/partials/’
},
viewPath: ‘views/email/’,
extName: ‘.hbs’
};
var sgTransport = require(‘nodemailer-sendgrid-transport’);
//using sendgrid as transport, but can use any transport.
var send_grid = {
auth: {
api_user: ‘api_user’,
api_key: ‘api_key’
}
}
var mailer = nodemailer.createTransport(sgTransport(send_grid));
mailer.use(‘compile’, hbs(options));
mailer.sendMail({
from: ‘[email protected]’,
to: ‘[email protected]’,
subject: ‘Any Subject’,
template: ’email_body’,
context: {
variable1 : ‘value1’,
variable2 : ‘value2’
}
}, function (error, response) {
console.log(‘mail sent to ‘ + to);
mailer.close();
});
[/js]

In the above code the ‘options’ variable is used to configure handlebar. I have specified the folder in which by handlebar files are present and .hbs as the extension of the files.

In the mailer.sendMail function template variable contains is the actual handlebar file name and context is the variables we are passing to handlebar template.

Lets now see our template files

File : views/email/template.hbs

[js]
{{>email/head}}
<body>
{{>email/header}}
{{{body}}}
{{>email/footer}}
</body>
</html>
[/js]

As per handlebars we have defined the partials and the main body content.

The main body file : views/email/email.body.hbs

[js]
<h4>Main Body Here</h4>
{{variable1}} <br/>
{{variable2}}
[/js]

In the main body file we are using the variables passing in the context.

Partials file : views/partials/email/header.hbs
[html]
<h4>Header Content</h4>
[/html]
Partials file : views/partials/email/footer.hbs
[html]
<h4>Footer Content</h4>
[/html]

So using handlebars and nodemailer, we see how we modularize email template and pass variables. This makes sending mails in your express project very easy.

You can put the mailer object in ‘req’ and use it through your application. Creating an easy middleware module would easily solve this purpose.

%d bloggers like this: