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 »

Hook up with Mailgun

by Jon Erik Solheim
|
Tutorials|API|
  • Share on Facebook
  • Tweet
  • Add to Pocket
  • Share on LinkedIn
  • Send email

We'll soon be entering 2017. And Email is still around.

This blog post shows how you can use a "serverless" Javascript Codehook to send email to people from a web page.

In this example you’ll learn how to use:

  • Mailgun API to send the actual email message
  • Handlebars HTML email template
  • Pages and Codehooks

Are you ready to learn some fun and useful stuff?

Setup

We will be using the example application found in the template catalog https://restdb.io/templates#webform.  This is a simple database with a single collection for storing web contacts and an IFrame plugin that you can use in any web page to collect emails and some additional data.

The example contact form looks like this screenshot:

contact form

The contact form is an IFrame to a Page created with the restdb.io form generator. This Page just POST data to the Contacts collection. The IFrame code is shown below:

<iframe src="https://www-webform-6bcb.restdb.io/webform" style="border: 0; width: 100%; height: 100vh"></iframe>

After some postings from a web page, your Contacts collection might look something like this screenshot:

contacts

The next section shows how we can use a Codehook to send a simple "thank you" email to users when they submit the contact form.

The email template

It’s important to send emails that works well on all major email readers, even across desktops and mobiles. In this example we have copied an open source version from Lee Munroe into a restdb.io Page.

The email template looks something like this screenshot:

mail template

Notice the {{name}} and {{input}} fields. These are Handlebars tags that will be replaced with database content before sending the actual email.

Mailgun setup

Before we can call Mailgun from our Codehook we need to add some variables to our global database settings in our restdb.io database.

{
  "formapikey": "<Your database CORS API key here>",
  "mailgun": {
    "from": "yourmail@yourdomain.com",
    "domain": "<Your mailgun domain name here>",
    "apikey": "<Your mailgun apikey here>"
  }
}

Replace these variables with your own before testing (in development mode: top left menu / Settings tab).

Codehook

In your database you’ll find the code for a collection under the Codehook tab (in development mode).

codehook

Because we want to send an email to new contacts in our database, we create a Codehook named beforePOST :

// on a new Contact form submit
function beforePOST(req, res){
    request({
        headers : {"x-apikey": context.apikey},
        url: "https://"+context.dbname+".restdb.io/views/mailtemplate"
    }, function(error, response, data){
        if (error) throw new Error(error);

        sendMail(req.body, data, function(){
           res.end(); 
        });        
    })
}

Notice the context variable that helps you with keeping apikeys and database name outside of your code.
This function first fetches the Handlebars HTML template from the database, then calls the sendMail subroutine, which in turn calls the Mailgun API.

The code for the ´sendMail´ is shown below:

// send mail using Mailgun API
function sendMail(body, templatestr, callback) {
    var YOUR_DOMAIN_NAME = context.settings.mailgun.domain;
    var apikey = context.settings.mailgun.apikey;

    var options = {
        method: "POST",
        url: "https://api:"+apikey+"@api.mailgun.net/v3/"+YOUR_DOMAIN_NAME+"/messages",
        headers: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        form: {
            from: context.settings.mailgun.from,
            to: body.email,
            subject: "Thank you",
            html: template(templatestr, {"name": body.email, "input": body.input})
        }
    };
    log.info("Sending mail with Mailgun...");
    request(options, function (error, response, body) {
      if (error) throw new Error(error);

      callback(body);
    });
}

The result

The finished result email looks like this example screenshot:

result email

Conclusion

Sending email is something most web applications have to deal with. Doing this with "serverless" cloud services like restdb.io can be done using webhooks in combination with Zapier or with Codehooks as demonstrated in this article. We chose to use Mailgun for this article, but a little tweaking should make it work with other providers as well (like Sendgrid). 
  • 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