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 »

Email from JavaScript with Gmail and database triggers

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

gmail

This blog post shows you how restdb.io simplifies sending emails via the Gmail mail service. We'll walk through a simple code example with a database trigger (serverless codehook) and a JavaScript that uses the mail API to send an email to a user.

If you need to send email from your application, there are many email providers to pick from (Mailgun, Sendgrid etc.).

But the advantage of using Gmail for your email transport is the almost zero technical setup to get it up and running.

Furthermore, it also lets you use your company email address in the sent-from and reply-to fields.

In this example we'll show you how to create a database trigger (codehook) that calls a JavaScript that sends email with the Gmail smtp transport.

Example

If you didn't know it already, restdb.io can trigger a JavaScript Codehook on a particular data event. E.g.: the screen shot below shows our example trigger afterPOST(…). This function is called on each POST to a database collection that we've called mailtrigger:

hook

The JavaScript code for sending email:

// Codehooks for mailtrigger
function afterPOST(req, res){
    log.debug("afterPOST",req.apikey);
    var htmlmail = "<h1>Alert!</h1>A record was created in the database <p>Best regards Acme</p>";
    var mailopt = {
        transport: "Gmail", // use Gmail transport
        to: req.body.email, // send to an email address from the database record
        subject: "Trigger email from you database", // Email subject
        html: htmlmail, // HTML formatted email message
        company: "Acme Inc", // Email footer
        sendername: "Acme support" // Email sendername, hides the email address
    };
    // send email with the API
    sendMail(mailopt, function(maildata){
        res.end();
    });   
}

Gmail settings

To use Google as SMTP transport you need to store your username and passord in a protected space, the database settings is perfect for this. In Development mode, navigate to the Settings tab (from the top level) and add the following JSON setting: settings tab

{
  …
  "restdb": {
    "Gmail-service": {
      "auth": {
        "user": "<your Gmail email address>",
        "pass": "<your Gmail password>"
      }
    }
  }
}

You must also set up your Gmail account to allow SMTP connections. Navigate on your Gmail account to: My account / Sign-in & security / Connected apps & sites. Then click the setting to ON for allowing access from your database:

gmail settings

You're all set.

Creating new (POST) records in our collection now sends emails from the JavaScript trigger. The example result is shown in the screen shot below:

email screen shot

This blog post has shown how easy it is to get started with sending emails from your application.

Note: using the Gmail for transport is avaliable for paid plans.

Links to further reading

  • JavaScript Codehooks
  • Mail API
  • Database REST API
  • 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
© 2023 restdb.io