simple online nosql database database with REST API and MongoDB queries
  • Features
  • Docs
  • Blog
  • Templates
  • Pricing
  • Contact
  • Sign Up
  • Log In

Blog

 The latest news and articles about restdb.io  

Database templates!
Don't forget to visit our template catalog! It's the fastest way to get up and running with a restdb.io database. View templates »

How we created a developer friendly email campaign tool using serverless only

by Knut Martin Tornes
|
Tutorials|Howto|
  • Share on Facebook
  • Tweet
  • Add to Pocket
  • Share on LinkedIn
  • Send email

Serverless is more There are tons of email marketing tools on the market, the most well-known being perhaps MailChimp and Campaign Monitor. These tools have varying prices and flexibility and each one has its own fanbase and target group.

We wanted to create a simple, developer friendly email campaign tool that we could use internally for our monthly newsletter at restdb.io (it's available as a database template you can play around with).

The requirements were these:

  • Edit the emails in Markdown syntax (to save time)
  • Possibility to upload email addresses using CSV/Excel or using an API
  • Ignore duplicate emails when uploading
  • Have an unsubscribe feature
  • Track emails sent and opens in realtime

And last, but not least: Everything should be written using serverless Javascript, HTML and NoSQL.

Difficult? Yes. Doable? Certainly! Read on.

Digging into the requirements

Creating a full-blown solution like this using serverless only can seem like a daunting task at first. However, we knew we could deal with most of the requirements by using the following built-in functionality of restdb.io:

  • Create collections (and API) for holding email templates and for recipients
  • Markdown field type (for editing email templates)
  • Unique field constraints (for the email field of recipients)
  • Import CSV/Excel
  • Codehook background jobs (for sending the emails robustly independent of the web client)
  • Mail API and unsubscribe functionality
  • Pages (for the dynamic HTML/JS user interface)
  • Realtime (for updating campaign stats live)

HTML User Interface

The HTML user interface was created using restdb.io Pages using both Javascript AJAX REST calls and server side database queries. It is actually quite simple and consists mainly of a link to restdb.io's data import, a simple dropdown for picking and previewing the template and a status of active campaigns and of course a button to run the campaign (not as cool as MailChimp's sweating monkey pushing the "Send" button, though).

front page

Clicking on the "Campaigns" tab shows past and running campaigns along with progress information and open percentage. One cool thing not possible to see here, is that the numbers are updated live. This is possible because you can listen to database REST events (in this case a PUT on the campaign job record).

running campaigns

We used the following Javascript/HTML libraries:

  • jQuery (to listen to events and manipulate the DOM)
  • Bootstrap (quick'n easy HTML UI)
  • Bootbox.js (alerts and dialogs - here used to confirm that the test email was sent)
  • bootstrap-select (for selecting the email template)
  • Lodash (utility library based on Underscore.js)
  • Marked (Markdown parser/compiler used for Markdown preview)
  • Superagent (for simple and elegant AJAX requests - used for all REST calls)
  • MobX (for simple state management)
  • restdb.io Javascript API (for the realtime updates)

To view (or modify) the source code, just install the template into a new database, activate "Developer Mode" and click "code editor" just below the "Dashboard" menu item (as shown in the image below).

Viewing the HTML source for the campaign tool

The Javascript source can be found in the index.js Page (shown below).

Viewing the Javascript client code

NoSQL Database Setup

We created basically just two collections (tables) to hold our data:

  • recipients (with field "email")
  • emails (with most important field "body" - containing the email template in Markdown syntax)

Other important collections are the built-in system_jobs and the email_outbound collections (more about these later).

Database Setup

Creating and editing emails

This functionality is supported directly with restdb.io. For any field on a collection which is of type text and "flavour" Markdown, will give you some editing help when creating a record in the database backend.

Creating the emails

Background Hooks for sending emails

One of the crucial parts of restdb.io which makes it possible to create an application like this, is Background Hooks.

We needed a background job for this application for a few important reasons:

  • POSTing thousands of records from the client would most likely fail
  • Sending emails can fail. We need to know which has been sent.
  • There are limits to how many emails it is possible to send in one batch
  • Serverless code are usually time-limited (to avoid them from hanging the servers)

A Background Hook is technically a record in a predefined system collection called system_jobs with a few predefined fields: crontab (a way to schedule the job - similar to *nix Crontab), active - a boolean indicating if the job is active and script - the Javascript code to be run.

For this solution, we added a few fields to the system_jobs collection: jobtype, status, newsletter. Jobtype was added to separate this type of job from (potential) others, status is the sending status, newsletter is a lookup relation to the newsletter to be sent.

Extra fields in the System Jobs collection

Putting it all together

To give you a better understanding of the application, we'll describe the basic flow of using it:

  • Add recipients (upload files or use REST API)
  • Write an email (a record in the email collection)
  • Test the email (default is the database owner). This uses client code to create HTML from the Markdown and then sends a POST (using Ajax) to the /mail endpoint of the database.
  • When clicking the Run! button, the application Ajax POSTs a new system_job record containing the job script and links the email to it (with a lookup relation). The job will run every minute and tries to send as many emails as possible before being "killed" by the timeout.
  • restdb.io Mail service automatically updates the email_outbound collection when emails are sent
  • The stats are updated live using the realtime API. We use a special collection countdata with codehooks and aggregation to get the tracking-status of sent and read emails.

The figure below shows a schematic overview of how the background job works.

The Email Campaign architecture

Conclusion

Creating a fully functional serverless application for sending emails is certainly doable as we've shown. It took us a few iterations for us to get the solution from the idea stage and into something we could actually use. The difficult part here was not writing the actual code, but to find out which code to write (somehow it's always like that!). The resulting code is not many lines in total, around 550 lines of Javascript in addition to a little HTML.

Next steps for you could be to test out the template and play around with the code (it's free and open source).

If you found this article interesting, please share with your fellow developers!

Links

  • Mail API
  • Codehook API
  • REST API
  • Email Campaign template
  • Share on Facebook
  • Tweet
  • Add to Pocket
  • Share on LinkedIn
  • Send email

All Posts


Search

Topics

  • API
  • Authentication
  • Case study
  • Features
  • Howto
  • Integrations
  • Newsletter
  • Productivity
  • Prototyping
  • Tutorials
  • User

restdb.io is a fast and simple NoSQL cloud database service. With restdb.io you get schema, relations, REST API and an efficient multi-user admin UI for working with data.

Our customers use it for backends, business databases, API-first CMS, data collection and much more. It is easy to get started with the free development plan.

Start Building »

  • Site Links
  • Home Page
  • About Us
  • Contact
  • Blog
  • Templates Catalog
  • Cloud Service
  • Features
  • Pricing
  • Terms & Conditions
  • Privacy Policy
  • Sign Up »
  • Documentation
  • Overview
  • Getting Started
  • Coding against the API
  • Utils
  • Security and Admin
© 2025 restdb.io