No Fax Machine? No Problem! SparkPost, Twilio, and Cloudinary Are Here To Save The Day!

I don’t know about you, but I don’t have a fax machine. Once in a blue moon, I have to send or receive a fax, so I was excited when Twilio shared an awesome way to receive faxes straight to your email using SparkPost. But that is only half the battle. I decided to build the ability to send faxes from an email so I would never need a fax machine again!

We’ll build a function so we can send a PDF attachment to [email protected]_DOMAIN, which will automatically fax the PDF to the phone number before the at sign (a.k.a. the local part, for all you email geeks out there). To do this, we’ll use SparkPost’s inbound functionality, Twilio’s Fax API, and Cloudinary to glue them together. We’ll receive an email to a Twilio function, pull off the attached PDF, save it to Cloudinary, and send it as a fax.

Sign Up and Configure Your SparkPost Account

The first thing you’ll need is a SparkPost account and a domain you want to use to receive mail (a.k.a. an inbound domain). You’ll also need to create an API key with permissions to read and write  inbound domains  and  relay webhooks 

Next, add the SparkPost MX records for your inbound domain. Once you verify that they are set up correctly, run the following cURL to add your inbound domain.

Sign Up For Cloudinary

Next, we’ll need a Cloudinary account to store the PDF we’ll fax. If you don’t know, Cloudinary is a powerful solution for storing, manipulating, and delivering all your media. Grab your cloud name, API key, and API secret and put them somewhere safe for later.

Create & Configure Twilio

We’ll use Twilio to consume the incoming emails and send the faxes. To get started, sign up for an account, and buy a phone number that can send and receive faxes.

Twilio’s serverless functions are perfect for this project. This isn’t a stateful application and it doesn’t need to be running all the time. Using one, we can quickly get set up and run our application.

We’ll need the following NPM modules for our function:

Let’s also add in our environment variables for Cloudinary and enable Twilio’s option to pass through our  ACCOUNT_SID and   AUTH_TOKEN so we can use their client library without worrying about our credentials:

The Code

First things first, let’s write our basic Twilio function and pull in our dependencies:

The   messages   variable is an object full of the messages SparkPost handed off to us. We’ll need to loop through the messages, pull off the attachment, save it to Cloudinary, and send off the fax.

Parsing The Email

You can see an example the payload SparkPost sends in the API documentation. The important parts for us are the  rcpt_to and the  content which will contain the raw email. These live inside  message.msys.relay_message.

We can pull out the phone number by splitting off the local part from the  rcpt_to  value – everything before the  @ .

We’ll also need to pull the PDF off of the message. To do this, we’ll parse the RFC 822 value using the  mailparser  library and return the content of the first PDF attached.

Saving The Attachment

Assuming we get an attachment, we’ll need to put it in an accessible spot for Twilio to pull it from. Enter Cloudinary. Using their Node library we can easily pipe the PDF and get the publicly accessible URL.

Sending The Fax

The last step for our code is to send the fax! Twilio makes this really easy. We need three values: the number we are sending to, the number we are sending from, and the media url. We’ll use the number we bought earlier to send for the “from” number. And we should have a phone number from the local part of the email and the media url from Cloudinary!

Create A Relay Webhook

The last piece of the puzzle it to tie our inbound domain from SparkPost to our Twilio function. Let’s create a relay webhook to pass all mail sent to our inbound domain onto our Twilio function. Copy your function path and pass it into this cURL request to create the relay webhook. Make sure that “Check for valid Twilio signature” is unchecked so that SparkPost can access it!

Send The Fax!! 🎉

Send an email over to [email protected]_DOMAIN with your fax attached as a PDF and it should go through!

You can set up receiving faxes for a fully working email fax machine with some guidance from our friend Patrick at Twilio! Feel free to reach out if you’ve got any questions and have fun faxing!


P.S. Before you launch this to the world, you’ll probably want to validate the phone numbers you’re sending to and add some security to who can send. I’d suggest a token in the email that you verify in the Twilio function.


Twilio Signal SparkPost Logo Hackathon Event

Why We’re Excited for Twilio Signal

1) Love talking about how bots, WebRTC, and IoT are changing the way we communicate? We do too! Not only do we love talking about it, we love developing apps and bots that help us communicate better (or at least in more fun ways), both as a company and with our community. We’d love to hear what you’re doing that’s different and cool.

2) We want to show off our Zap. Let’s get real. APIs are the star of this event, with Twilio’s coming out on top. We’ve recently linked SparkPost and Twilio, because they really are better together. With this Zap, every new SMS message received by your chosen Twilio message will automatically trigger an email notification from SparkPost.

3) Our killer limited edition client library stickers. We’ve got 8 awesome client libraries to show off, and now the stickers to go along with them! From the famous elePHPant to the purplish hues of Elixir, we’ve got one for each of them. Choose your language and represent with pride!

SparkPost Client Library Stickers for Twilio Signal

4) Last but not least, we want to meet you! The thing we love most about conferences is being able to meet all of you face-to-face and get direct feedback on what you love about SparkPost, as well as what we could be doing better. Our product is better because of your ideas, and we want to hear them!

So come find us Tuesday and Wednesday, May 24-25, in the expo hall. We’ll be there from 8:00 am – 7:30 pm both days. Come on by to talk shop, grab some new swag, pick up the sticker to represent your client library pride, and get a tour of the latest features.

Can’t make it to Twilio Signal? You can still build awesome things with the SparkPost API! Be sure to check out our client libraries, support docs, and more resources at the Developer Hub, and don’t forget to join our community Slack group! We hope to see you at an event in the near future.

Community Manager, SparkPost