Receive emails and attachments with Webhook Push

Receive emails with http webhooks. Webhooks let you respond to inbound email streams in real time on your server or within an application.

email read on server

MailSlurp provides many ways to receive an email. One popular method is using webhooks. Webhooks enable you to receive emails and attachments directly to your server when they arrive without the need to call MailSlurp directly. Webhooks must be created using the dashboard or API and assigned to an inbox. You can attach a single webhook to multiple inboxes if you wish to receive emails and attachments across many addresses.

See the Webhoook developer documentation for model schemas, event types and examples.

About webhooks

Email API webhook

Every MailSlurp inbox can be connected to a webhook. Webhooks are rules that forward a payload to an HTTP endpoint that is publicly accessible on your server or system. MailSlurp will post a JSON payload to your server when a webhook event is triggered.

Webhook event types

Webhooks can be setup to trigger for different events:

  • NEW_EMAIL (or EMAIL_RECEIVED) - triggered when an inbox receives a new email
  • NEW_CONTACT - triggered when a new recipient or sender is encountered on an incoming email
  • NEW_ATTACHMENT - triggered when a new attachment is received in an email

Payloads for webhooks

Each Webhook has an associated triggering event. The event type determines what type of JSON payload will be sent to your server.

Webhook response

When webhooks are triggered a JSON payload will be sent via HTTP POST to your server URL. Your server must return a 200 or 201 response status within 30 seconds. If so the webhook is marked as successfully processed. If not the webhook will be placed in a queue and retried until a success is obtained.

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.

Use the WebhookControllerAPI endpoints or the methods available in the SDKs to create and manage webhooks.

mailslurp.webhookController.createWebhook(inboxId, {
    url: "https://my-server.com/webhook",
    eventName: "NEW_EMAIL"
})

You can also create a webhook using the dashboard:

email webhook

You can share a webhook across multiple inboxes by attaching the same webhook ID to each new inbox you create.

Monitoring webhook results

In the MailSlurp dashboard you can monitor your webhook history to see how your webhooks are being handled.

email testing

Testing webhooks

We recommend you use a service like ngrok.io to test webhooks. It allows you to tunnel a localhost to a publicly accessible url so that MailSlurp can call your local test machine.

Idempotency

Webhooks are guaranteed to be sent at least once. Efforts are made to deliver only once but they may occasionally be sent multiple times due to the distribute queue system used. To mitigate duplication use the event messageId to differentiate between events. Store the messageId and acknowledge but do not process the event if you encounter the same ID again.

Usage

Expose an endpoint on your server and create a MailSlurp webhook using the URL. Listen for incoming events and return a 200.

NodeJS 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);

Using SDK models for deserialization

The WebhookPayload class is included in the MailSlurp SDKs and can be used to deserialize the webhook payload. For instance:

Webhook Payloads

See the developer webhook documentation for payload schemas.