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 to automate incoming e-mails with a restdb.io database

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

incoming-email-automation E-mail is an important and integrated part of many systems. restdb.io has had decent support for automating outbound e-mail for a while now (more details in the restdb.io docs).

One of the new features, inbound email, opens up new possibilities for automation involving e-mail. All databases now get their own e-mail address and incoming e-mails are now stored in a collection for further processing.

In this short article, we will show you how to enable the feature together with an example of how to set up a codehook (trigger function) when the e-mail is received. We will also demonstrate how to use the new console debug output to see what's going on inside the codehook.

Enabling the e-mail feature

In your list of databases, find the button "Users and Settings" next to the database you want to set up. Click it to open the settings.

click users and settings

Next, select the "API" tab and scroll all the way down until you see a header with "Incoming email address key".

select API tab

Click the "Generate" button to create an incoming e-mail address for your database. The database can now receive e-mails using this address or this address with a prefix, for example leads+d85587e9a77c@mg.restdb.ws. This prefix can be useful if you want to distinguish between various incoming e-mails and want to process them differently.

scroll down API tab

Incoming E-mail collection

Opening the database backend in "Developer" mode, you can select the system collection email_inbound and see any e-mails received. Next up is writing a restdb.io codehook to process the incoming e-mail.

incoming email collection

Writing a simple codehook to process the incoming e-mail

We will implement an afterPOST codehook on the email_inbound collection to process our incoming e-mail. This codehook is triggered after an email has been received and in our example, we will insert a new record into a leads collection and then delete the record from the email_inbound collection.

Working with restdb.io codehooks can be difficult if you're not seeing what's going on inside it. There are two ways to find out what's going on. In "Developer Mode", you can either open the REST inspector to do this or open the browsers console and type in: restdb.startDebug() to start receiving log output from all codehooks.

In order to see what the incoming email feature POSTs to the incoming_email collection, we are going to send an email to our db's address (leads+d85587e9a77c@mg.restdb.ws). When the email is received, the afterPOST codehook is invoked.

Adding the following codehook to incoming_email will let us see what we receive from the incoming email functionality in restdb.io:

async function afterPOST(req, res){
    // log to console to see structure
    console.log("POST body received:");
    console.log(JSON.stringify(req.body,null,2));
    res.end({});
}

the debug console

As we can see from the screen shot of the console, we receive a JSON object with the following fields:

  • from
  • subject
  • body
  • from-name
  • to
  • stripped-text
  • body-plain
  • stripped-html

body contains the full html of the e-mail and body-plain contains only the text. For our codehook, we'll use the subject, body-plain and the from field.

editing the codehook

We will now replace our "debug version" of the codehook with the following snippet:

async function afterPOST(req, res){
    const {from, subject} = req.body;
    const email_body = req.body["body-plain"];
    try{
        // add new record to "Leads" collection
        await db.post("/rest/leads",{title: subject, description: email_body, "contact-email": from});
        // delete the record from "Email_inbound" collection
        await db.delete(`/rest/email_inbound/${req.body._id}`);
    }catch(error){
        console.log("Error:", error);
    }
    res.end({});
}

The codehook extracts the fields we need and then POSTs a new record in the leads collection. The system record in the email_inbound collection is then deleted.

Conclusion

restdb.io provides powerful cloud-based mechanisms for automation in a data-centric and serverless way. The built-in REST codehooks and background jobs powered by Javascript has now been extended with incoming e-mail. Based on the code snippets in this article, you should be able to try this out right away.

  • 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