Every MailSlurp inbox can be connected to a webhook. Webhooks are rules that forward emails to an HTTP endpoint that is publicly accessible on your server or system. MailSlurp will post a JSON payload that contains the emailId and inboxId to your system every time an email is received. You can then fetch the full email, forward, parse the contents, stream attachments and more!

# Creating a webhook

First create an inbox. Then you can create a webhook online in the webapp or using the API or SDKs. You must define a URL for the webhook that can be called by MailSlurp servers using HTTP POST method.

# The payload

Each time an email is received a JSON payload will be sent to your URL via HTTP POST. Here is a sample of what that looks like:

{
  "inboxId": "...",
  "emailId": "...",
  "createdAt": "...",
  "name": "myWebhook"
}

The exact JSON-Schema of the webhook notification payload is defined below:

{
  "type": "object",
  "id": "urn:jsonschema:com:mailslurp:lib:models:WebhookNotificationObject",
  "properties": {
    "inboxId": {
      "type": "string",
      "required": true,
      "format": "uuid"
    },
    "createdAt": {
      "type": "string",
      "required": true,
      "format": "date-time"
    },
    "emailId": {
      "type": "string",
      "required": true,
      "format": "uuid"
    },
    "name": {
      "type": "string"
    }
  }
}

# Express example

Let's see how a webhook fits together with an example Javascript server using Express.

Note: you can use any framework or language you like with webhooks.

const express = require("express");
const app = express.createServer();
app.use(express.bodyParser());

/**
 * define your endpoint
 * here your webhook url should include the full protocol and domain.
 * i.e.: https://myserver.com/my-webhook-endpoint
 */
app.post("/my-webhook-endpoint", function(request, response) {
  // access the data on request body
  console.log(request.body.inboxId);

  // return a 2xx status code so MailSlurp knows you received it
  response.sendStatus(200);
});

app.listen(80);