How to send email to users upon signup using Realtime and ServerCode


One of the most important tactics when trying to engage your users is to send them a welcome email when they sign up. This HowTo will show you how to achieve this using three Appstax components:

  • DataStorage to store users
  • Realtime channels to receive notifications when users sign up
  • ServerCode to securely manage the realtime listening and send email


  • Before you can use the features in this HowTo you will need to enable them. Go to the Admin UI and under you app Settings, enable each of the three components.
  • We will assume that you are already familiar with using the Appstax CLI. If you are not, take a moment to familiarize yourself with it.
  • Create a new server module by running appstax server create.
  • The example code uses a Mandrill account to send emails. For this to work you will need to create an account with Mandrill if you don't have one already. If you have an account with some other similar service, you can of course modify the example code accordingly.

Set up server

Once you have created an app, downloaded the CLI, initialized an app (appstax init) you will need to install the following Node modules in your project directory. Make sure there exists a node_modulesdirectory in your project directory, and run:

  • npm install --save mandrill-api
  • npm install --save express
  • npm install --save appstax

Then create the file app.js and paste the following code into the file and save it.

var express = require('express');
var app = express();
var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill('<your mandrill account id>');
var appstax = require("appstax");
appstax.init("<your appkey>");

function sendEmail(message) {
        mandrill_client.messages.send({"message": message}, function(result) {
    }, function(error) {
        // Mandrill returns the error as an object with name and message keys
        console.log('A mandrill error occurred: ' + + ' - ' + error.message);
var server = app.listen(3000, function () {
    var ch ="objects/users");
    ch.on("object.created", function(event) {
        var message = {
            "html": "<pre>" + JSON.stringify( + "</pre>",
            "text": "Example text content",
            "subject": "example subject",
            "from_email": "",
            "from_name": "Example Name",
            "to": [{
                    "email": "",       // <-- Change this!
                    "name": "Jane Doe",
                    "type": "to"
            "headers": {
                "Reply-To": ""


  console.log('Example app listening');

Change the email where indicated, to reflect your email address.

Then run appstax deploy. This uploads your code (specifically your app.js and your node_modules directory.) and restarts your server.

Creating a user and receive email

Go to the Admin UI and manually create a user. Then wait for an email.

If you do not receive an email, check the server log using appstax server logs 100 to download the last 100 entries in the server log. It should show a JSON structure like this:

    html: '<pre>{"sysCreated":"2015-10-14T11:44:15 028820239Z","sysObjectId":"AXOrYMcDRm3N","sysUpdated":"2015-10-14T11:44:15.028820239Z","sysUsername":"balle15"}</pre>',
    text: 'Example text content',
    subject: 'example subject',
    from_email: '',
    from_name: 'Example Name',
    to: [ { email: '', name: 'Jane Doe', type: 'to' } ],
    headers: { 'Reply-To': '' } 
    { email: '', status: 'sent', _id: 'a2a1bf1a99114a43afc607c81564b953', reject_reason: null } 

What did we do?

The heart of the code is subscribing to the realtime channel objects/users. This sets a listener for all objects in the collection users.