{ 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!

SES Email Trigger

1. Creating SailsJs Project

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

2. Installing Packages

To install the package use the below command

npm install node-ses --save
npm install ejs --save

 

3. Creating Email Template

We will also need an email template for sending the email.
Create welcome.ejs file in your views directory

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

mail is the variable name which will be passed to the template

4. Email Service

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

   sendingSESMail: function(path, data, email, subject) {
        var ses = require('node-ses');
        var ejs = require('ejs');
        var s3Key = ""; //add your s3 key here
        var s3Secret = ""; //Add your s3 secret here
        var s3Region = ""; //Add your s3 Region here
        var client = ses.createClient({ key: s3Key, secret: s3Secret, amazon: s3Region });
        ejs.renderFile(
            path,
            data,
            {},
            function(err, str){
                client.sendEmail({
                    to: email,
                    from: "no-reply@sailsit.com", //from email
                    subject: subject,
                    message: str
                }, function (err, data, res) {
                    sails.log.debug(data);
                    sails.log.debug(err || "Email is sent");
                });
            });
    }

Note: In this function, you need to configure your S3 details

5. Creating Controller

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

sails generate controller SesEmail

Paste the below code in your controller

   triggerEmail: function (req, res) {
        var mailBody = {
            recipientName: "Sailsit Develoepr",
            senderName: "Sailsit Admin"
        };
        SesEmailService.sendingSESMail("views/welcome.ejs", mailBody, "developer@sailsit.com", "SailsIt Welcome", function (err, message) {
            if(err) {
                sails.log.debug(err);
                res.serverError(message);
                return;
            }
            res.status(200);
            return res.send({
                "message": message
            });
        })
    }

 

6. 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"

 

7. 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 sendingSESMail() method in SesEmailService.js

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

You can find the source code for the tutorial at below URL
SailsSESEmail