This guide will show you how you can handle incoming text messages and forward them to an email address.
We will do that by building a server that receives network requests from SignalWire whenever an SMS is received, and then sends an email.
What do I need to run this code?
You will also need a SignalWire phone number which is configured for webhooks. Within the Phone Numbers tab of your SignalWire portal you will be able to acquire a phone number: you should configure its "Messaging Settings" to handle messages using a "LaML Webhook". Then, you need to specify the public URL where your application is listening to. You can use ngrok to get a public address, see our guide on How to Locally Test Webhooks with Ngrok. Regardless of what your public address will be, this application listens for POST requests on the
/sms-webhook route, so your URL will be something like
How to Run the Application
Build and Run on Docker
You can either:
Use our pre-built image from Docker Hub:
docker pull signalwire/snippets-text-to-email:python
or, build your own Docker image:
- Build your image
docker build -t snippets-text-to-email .
- Run your image
docker run --publish 5000:5000 --env-file .env snippets-text-to-email
- The application will run on port 5000
Build and Run Natively
To run the application, execute
export FLASK_APP=app.py then run
Step by Step Code Walkthrough
Within the Github repository you will find several files, but we're interested in:
The first one (app.py) contains the full implementation of the application. The second one contains some environment variables for configuration.
Setup Your Environment File
- Copy from example.env and fill in your values.
- Save a new file called .env
Your file should look something like this.
# MailGun domain associated with your MailGun account
# MailGun token associated with your MailGun Account
# Send Email From Address
# Send email to address for administrator notifications
# Email subject for admin notifications
EMAIL_SUBJECT=Forward Text To Email
This code is actually very simple - you need only one route and one function! The first step is to define a function
send_email(body) that will utilize the MailGun API to send an email using the variables from our
# Send email with MailGun
"https://api.mailgun.net/v3/" + os.environ['MAILGUN_DOMAIN'] + "/messages",
"text": body })
Our only route
/sms-webhook will listen for POST requests from incoming messages and will use the JSON form data as the body of the email.
# Listen on route '/sms-webhook' for GET/POST requests
# Forward incoming form data to email
When dealing with SMSes in large scale, you'll often want to convert them into a more manageable format. This guide has shown one possible way to achieve that goal by converting them to emails, using a remarkably short script of code.
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!