Hi Today we here to implement Email sending in Sails.js using npm package sails-hook-email and Mailgun service.

Assuming that you have created basic sails app with user controller.

Install sails-hook-email package

npm install sails-hook-email --save

2. Create a email config file under config folder in the sails app

3. Create Mail service

// api/services/Mailer.js module.exports.sendWelcomeMail = function(obj) {

sails.hooks.email.send(

“welcomeEmail”,

{

Name: obj.name

},

{

to: obj.email,

subject: “Welcome Email”

},

function(err) {console.log(err || “Mail Sent!”);}

)

}

4. Create some action to consume the mail service.

// api/controllers/UserController.js module.exports = {

create: function (req, res) {

User.create(req.body).exec(function (err, user) {

if (err) {

return res.json(err.status, {err: err});

}

if (user) {

Mailer.sendWelcomeMail(user); // <= Here we using

res.json(200, {user: user});

}

});

}

};

5. Finally template for email

// api/emailTemplates/welcomeEmail/html.ejs <p>Dear <%=Name%>,</p>

<br/> <p>Thank you for SignUp.</p>

Cheers