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

Contact