{ sailsit }

Sails Js Tutorials for Beginners and Forum

Have a Question?

If you have any question you can ask below or enter what you are looking for!

SMTP Email Trigger

1. Creating SailsJs Project

Follow the below tutorial for setting up SailsJs
Setting up sails project

2. Installing Packages

For creating jobs we need to install sails-hook-email package, this package is developed by Balderdash
To install the package use the below command

npm install sails-hook-email --save

 

3. Configuring SMTP Email

Create email.js in your config directory and add the below code

module.exports.email = {
  service: 'Gmail',
  auth: {user: 'anyGmailEmail@gmail.com', pass: 'passwordOfGmail'},
  testMode: false
};

Change the user and password to your gmail emailId and password
Note: Above email/password will be used for sending the email

4. Creating Email Template

We will also need an email template for sending the email.
Create directory emailTemplates inside views directory (if not exist)
Now create a directory sampleEamil inside your emailTemplates
Add file html.ejs which will contain your email template. For this example we will be using a very basic HTML tag

<p>Dear <%=recipientName%>,</p>
<br/>
<p><em>Welcome</em> to SailsIt.</p>
<p><%=senderName%></p>

 

5. Email Service

Now for triggering email, create SampleEmailServices.js file inside services directory and paste the below code

   sendMail: function(mailBody, subject, template, cb) {
        sails.hooks.email.send(
            template,
            mailBody,
            { to: "developer@sailsit.com", subject: subject},
            function(err) {
                if(err) {
                    sails.log.debug(err);
                    cb(err, "Email not sent");
                }
                cb(null, "EMail Sent");
            }
        )
    }

 

6. Creating Controller

Let’s create a controller to trigger the email service.
You can create a controller using below command

sails generate controller SampleEmail

Paste the below code in your api/controllers/SampleEmailController.js

   triggerEmail: function (req, res) {
        let mailBody = {
            recipientName: "Sailsit Develoepr",
            senderName: "Sailsit Admin"
        };
        SampleEmailServices.sendMail(mailBody, "Welcome to sailsIt", "sampleEmail", function (err, message) {
            if(err) {
                sails.log.debug(err);
                res.serverError(message);
                return;
            }
            res.status(200);
            return res.send({
                "message": message
            });
        })
    }

 

7. Creating Route

Let’s create a route for triggering the controller function
Paste the below route in your config/routes.js

"GET /api/email/trigger": "SampleEmailController.triggerEmail"

 

6. Running Sails

Now you can start your sails project using below command

sails lift

 

Now you can hit the below route in your browser, which will trigger a welcome email to email which is defined in your sendMail() method in SampleEmailServices.js

http://localhost:1337/api/email/trigger

You can find the source code for the tutorial at below URL
Sails-SMTP-Email