Forwarding Inbound Faxes to Email
This short and simple guide will show how you can use the SignalWire Python SDK and the MailGun API in order to forward your incoming SignalWire faxes to email. You can easily bridge this older technology by allowing faxes to be delivered to your inbox with only a few lines of code.
What do I need to run this code?
How to Run the Application
To run the application, execute
export FLASK_APP=app.py then run
You may need to use an SSH tunnel for testing this code if running on your local machine. – we recommend ngrok. You can learn more about how to use ngrok with SignalWire here.
To use this Script, you need to expose it to the web (either through
ngrok or by hosting it on a server) and use it as a webhook for handling incoming calls under phone number settings. For example, this is what it looks like if you use an ngrok tunnel to the script. For this script, you would use the given ngrok URL and the
/fax-webhook route, like this
Step by Step Code Walkthrough
Configuring Your Environment File
- Copy from example.env and fill in your values
- Save new file called .env
Your file should look something like this
## This is the full name of your SignalWire Space. e.g.: example.signalwire.com
# Your Project ID - you can find it on the `API` page in your Dashboard.
# Your API token - you can generate one on the `API` page in your Dashboard
# The phone number you'll be using. Must include the `+1` ,
# MailGun domain associated with your MailGun account
# MailGun token associated with your MailGun Account
# Send Email From Address
# Send email To address
# Email subject
EMAIL_SUBJECT=Forward Fax To Email
Configuring the main script
We start with the necessary imports and instantiate a Flask app:
from dotenv import load_dotenv
from flask import Flask, request
app = Flask(__name__)
Next, we need to define a function that will use the MailGun API to send the email. You can read more about sending with the MailGun API in their documentation here.
This function is very simple and copied exactly from their documentation - no need to make any difficult changes! Set up the API token, from address, to address, and subject line in your
.env file. We will show you how to set that up further down in the guide! The body will be passed in as a variable to the function when an incoming fax is received and the webhook is used.
"https://api.mailgun.net/v3/" + os.environ['MAILGUN_DOMAIN'] + "/messages",
We also need to define the route we will be using to accept incoming GET/POST requests. In this route, we will call our
send_email function created above with the formatted form data in the fax as the body. We will then return "200" to signify it was successful.
Finally, we run the application:
if __name__ == "__main__":
This guide demonstrates how easy it can be to forward your faxes to email using the Mailgun API and SignalWire Python SDK allowing for review of faxes from anywhere.
Sign Up Here
If you would like to test this example out, you can create a SignalWire account and space here.
Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance!