Extracting Email Attachments From Relay Webhooks

April 29, 2017 Contributors

Relay webhooks are a way to receive email to your own HTTP endpoints. For each email sent to your inbound domain, SparkPost will call your endpoint with a JSON request containing the message with headers already parsed out into an array. Many of our users also need to extract file attachments from the email.  Here are some outline steps and sample code showing how to receive emails via relay webhooks and the process of extracting email attachents.

# Prerequisites

Before receiving email via relay webhook, the following setup steps are required:

  1. Register your domain name in the DNS
  2. Configure your domain as an inbound domain on your SparkPost account (details here)
  3. Prepare a publicly-accessible HTTP endpoint capable of receiving POST requests with JSON-formatted bodies. Sample code for this step is provided below.

# Retrieving Attachments Through Your Relay Webhook

  1. Receive POST request from SparkPost relay webhook service
  2. Parse email body using MIME library for your chosen language / framework
  3. Extract attachments from MIME email structure

# PHP Relay Webhook Attachments Sample Code

# Python Relay Webhook Attachments Sample Code

# Node.js Relay Webhook Attachments Sample Code