X
X

Login

Login if you already have an account

LOGIN

Register

Create a new account. join us!

REGISTER

Support

Need some help? Check out our forum.

FORUM

FYI – With FatFractal you can send text or html emails programmatically using our JS Server-side SDK.

As a developer, I want to be able to easily send emails from my backend so that I can validate users, create email reports and alert others to important information


There are many use cases where sending an email from your application backend. One is to notify someone when some data has changed. Another is for the purposes of validating a user. This post will cover how to do both with your FatFractal backend.

Text-only emails

The sendSMTPEmail method (docs) will send text-only email messages from an account that you specify. For example, if you want to send a notification email to alert you when a new object is stored – say an Order, is received by your app.

First, define an EventHandler (what’s this?) as follows that will extract info from the new Order object, get some additional information from your backend and the create a notification email to whomever you want to alert them that a new order was created. This should go in a file in the ff-scripts directory that was created when your app was created (more about this), in this case we will call it EventHandlers.js.

function notifyNewOrder() {
var order = ff.getEventHandlerData();
var user = ff.getObjFromUri("ff/resources/FFUser/" + order.createdBy);
var name = user.firstName + " " + user.lastName;
var msgSubject = "New Order notification";
var msgString = "Order " + order.amount + " was created by " + name;
ff.sendSMTPEmail("<mailserver>", "465", "true", "465", “<username>”, “<password>”, “<fromAddress>”, “<toAddress>”, msgSubject, msgString);
}

In your FFDL (what’s this?), you then define your EventHander like this:

CREATE HANDLER notifyNewOrder ASYNC ON /Order CREATE AS javascript:require (‘scripts/EventHandlers.js’).notifyNewApp();

HTML emails

The sendEmail method (docs) accepts a set of parameters and will send your html content via an email account that you specify. For this example, we will use a slightly more complicated example – we will send out

function sendWelcomeEmail() {
var user = ff.getEventHandlerData();
var subject = “Welcome to Foo - we are so glad you joined us”;
var html = getHtmlWelcomeMessage(user);
try {
var emailData = {
host:"<mailserver>",
port:"<port>",
auth:<true/false>,
authPort:"<port>",
username:"<username>",
password:"<password>",
from:"<fromAddress>",
to:user.email,
subject:subject,
text:text, html:html
};
emailData = JSON.parse(JSON.stringify(emailData));
ff.sendEmail(emailData);
} catch (error) {
throw "Caught exception: " + error + " while sending email";
}
}

And, include the FFDL for this EventHandler as well.

CREATE HANDLER validateUser POST ON /FFUser CREATE AS javascript:require (‘scripts/EventHandlers.js’).sendWelcomeEmail();

The above makes use of a function called getHtmlWelcomeMessage which fetches a template HTML file that I have included in the webapp directory of my app, replaces marked elements with personalized content and returns the HTML content using the http client that is available to your server side js code.

function getHtmlWelcomeMessage (user) {
var hc = require('ringo/httpclient');
var htmlMessage = hc.get(ff.getHttpAppAddress() + '/welcome_email.html').content;
htmlMessage = htmlMessage.replace("___FIRST_NAME___", user.firstName);
htmlMessage = htmlMessage.replace("___LAST_NAME___", user.lastName);
return '' + htmlMessage;
}

That’s it – super easy to send email content to your users or for internal notifications/ reports or whatever you want!

Happy coding!

Kevin

For more details about FFDL, the documentation is here.

For more details about the Server-side JS SDK, the documentation is here.

For more about EventHandlers, the documentation is
Event Handers Reference

FYI – it is easy to integrate your backend with other web services

As a Developer, I want to integrate with other web services so that I can offload the integration tasks from my client to my backend.

Many times, you may want to utilize some other web service for the purposes of backend integration. FatFractal makes this really easy.

Let’s say you want to interact with SalesForce data from your backend…

FatFractal support the Common.js standard which includes an http client that let’s you do just about anything you want.

Example:

Here is some code that interfaces to to securely retrieve some information from a SalesForce API:

var hc = require('ringo/httpclient');

exports.getQs = function() {
var loginUrl = "https://login.salesforce.com/services/oauth2/token";
var apiVersion = "v25.0";
var username = "someUsername";
var password = "somePassword";
var securityToken = "someSecurityToken";
var clientId = "someClientId";
var clientSecret = "someClientSecret";

function LoginSF(username, password, securityToken, clientId, clientSecret) {
var data = {
grant_type: "password",
client_id: clientId,
client_secret: clientSecret,
username: username,
password: password + securityToken
};
var response = JSON.parse(hc.post(loginUrl, data).content);
return response;
}

function QuerySF(instanceUrl, accessToken, queryString) {
var url = instanceUrl + "/services/data" + apiVersion + "/query";
var data = {q: queryString};

var request = {
url: url,
data: data,
method: "GET",
headers: {
Authorization: "Bearer " + accessToken
}
};

var response = JSON.parse(hc.request(request).content);
return response;
}
// perform query
var loginResponse = LoginSF(username, password, securityToken, clientId, clientSecret, true);
var instanceUrl = loginResponse.instance_url;
var accessToken = loginResponse.access_token;
var queryResponse = QuerySF(instanceUrl, accessToken, "SELECT A__c, B__c, C__c FROM D__c");

var records = queryResponse.records;
for (var i = 0; i < records.length; i++) {
// DO SOME STUFF
// E.G. RETURN SOME DATA (SERVER EXTENSION)
// OR TAKE SOME ACTION (EVENT HANDLER)
}
}

What this does is define a function that can be used with an Event Handler or as an API extension with a Server Extension that logs in to an API on SalesForce.com, retrieves an instance_url and access_token that are need to then to execute a query and retrieve a response.

Many other things can be done with this powerful client – have fun!

Kevin

FYI – it is easy to integrate your backend with other web services (for example, SalesForce.com)

As a Developer, I want to integrate with other web services so that I can offload the integration point from my client code to my backend for better performance, security and reliablility.

Many times, you may want to utilize some other web service for the purposes of backend integration. FatFractal makes this really easy. FatFractal supports the Common.js standard which includes an http client that let’s you do just about anything you want.

Let’s say you want to interact with SalesForce data from your backend…

Example:

Here is some code that securely retrieves some information from a SalesForce API that could be used in either a Server Extension of Event Handler:

var hc = require('ringo/httpclient');

exports.getQs = function() {
var loginUrl = "https://login.salesforce.com/services/oauth2/token";
var apiVersion = "v25.0";
var username = "someUsername";
var password = "somePassword";
var securityToken = "someSecurityToken";
var clientId = "someClientId";
var clientSecret = "someClientSecret";

function LoginSF(username, password, securityToken, clientId, clientSecret) {
var data = {
grant_type: "password",
client_id: clientId,
client_secret: clientSecret,
username: username,
password: password + securityToken
};
var response = JSON.parse(hc.post(loginUrl, data).content);
return response;
}

function QuerySF(instanceUrl, accessToken, queryString) {
var url = instanceUrl + "/services/data" + apiVersion + "/query";
var data = {q: queryString};

var request = {
url: url,
data: data,
method: "GET",
headers: {
Authorization: "Bearer " + accessToken
}
};

var response = JSON.parse(hc.request(request).content);
return response;
}
// perform query
var loginResponse = LoginSF(username, password, securityToken, clientId, clientSecret, true);
var instanceUrl = loginResponse.instance_url;
var accessToken = loginResponse.access_token;
var queryResponse = QuerySF(instanceUrl, accessToken, "SELECT A__c, B__c, C__c FROM D__c");

var records = queryResponse.records;
for (var i = 0; i < records.length; i++) {
// DO SOME STUFF
// E.G. RETURN SOME DATA (SERVER EXTENSION)
// OR TAKE SOME ACTION (EVENT HANDLER)
}
}

Note the inclusion of the httpclient at the top of the source:

var hc = require('ringo/httpclient');

The rest defines a function – “getQs” that can be used with an Event Handler or as an Server Extension that logs in to an API on SalesForce.com, retrieves an instance_url and access_token that are required to then to execute a query and retrieve a response.

Many other things can be done with this powerful client – have fun!

For more information on our Server-Side Javascript SDK, see here.

For more information on Event Handlers on your backend, see here.

For more information on Server Extensions on your backend, see here.

Kevin

Contact