Email to Fax
β± 60 minutes build time.
π§° Clone the sample application from our GitHub repo
π¬ Check out our on-demand webinar walking through this tutorial.
In this tutorial, you'll learn how to use the Telnyx Fax API to receieve faxes to your email in Python.
Our Programmable Fax API, combined with our Numbers API, provides everything you need to build a robust call tracking application:
The Numbers API enables you to search the Telnyx phone number inventory in real time; filtering by Area Code, City/State, and more to find the perfect local number for your use-case. Call Control enables you to quickly setup dynamic forwarding numbers, toggle dual-channel recording, join/leave dynamic conferences, and pull post-call analytics. By following this tutorial, you'll be able to:
- Configure a Telnyx portal account.
- Set up a Telnyx Fax profile and an outbound voice profile.
- How to send a fax to your email using Telnyx.
- How to send a fax using your email and to Telnyx Fax profile.
What you'll need to get started with email to fax β
- A Telnyx account- take a minute to sign up to our self-service portal.
- A Telnyx phone number that's enabled with a Telnyx Fax Appliction and a Telnyx Outbound Voice profile .
- Ability to receive webhooks (with something like ngrok)
- Python and PIP installed
- An AWS Account setup with proper profiles and groups with IAM for S3. Check out the Quickstart for more information.
- Finally you'll need a Mailgun account . If you're planning on using this guide to build an email to fax application you'll need an account with the ability to setup inbound routes .
Create a Telnyx Mission Control Portal account β
To get started, you'll need to create an account . Verify your email address and you can log into the Mission Control Portal to get started.
Usage β
The following environmental variables need to be set:
Variable | Description |
TELNYX_API_KEY | Your Telnyx API Key |
TELNYX_PUBLIC_KEY | Telnyx Public Key |
TELNYX_S3_BUCKET | The name of the bucket to upload the media attachments |
TELNYX_FAX_CONNECTION_ID | The connection ID for your Fax Applications |
MAILGUN_API_KEY | Your Mailgun API Key |
MAILGUN_DOMAIN | Your Mailgun Domain. |
PORT | Defaults to 8000 The port the app will be served |
.env file β
This app uses the excellent python-dotenv package to manage environment variables.
Make a copy of .env.sample and save as .env and update the variables to match your creds.
TELNYX_PUBLIC_KEY="+kWXUag92mcUMFQopVlff7ctD/m2S/IoXv+AlI1/5a0=" TELNYX_API_KEY="KEYI" TELNYX_S3_BUCKET=telnyx-mms-demo TELNYX_FAX_CONNECTION_ID=36092346987 MAILGUN_API_KEY="123-432-123" MAILGUN_DOMAIN="sandbox367c5ec1512d458e95f5e5c60f5fe97a.mailgun.org" PORT=8000
Callback URLs for Telnyx application β
Callback Type | URL |
Fax Callbacks | {ngrok-url}/faxes |
Email Callbacks | {ngrok-url}/email/inbound |
Install β
Run the following commands to get started:
$ git clone https://github.com/team-telnyx/demo-python-telnyx.git
Ngrok β
This application is served on the port defined in the runtime environment (or in the .env file). Be sure to launch ngrok for that port:
./ngrok http 8000
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online Account Little Bobby Tables (Plan: Free) Version 2.3.35 Region United States (us) Web Interface http://127.0.0.1:4040 Forwarding http://your-url.ngrok.io -> http://localhost:8000 Forwarding https://your-url.ngrok.io -> http://localhost:8000
Connections ttl opn rt1 rt5 p50 p90 0 0 0.00 0.00 0.00 0.00
At this point you can point your application to generated ngrok URL + path:
(Example: http://{your-url}.ngrok.io/faxes
).
Run β
High level code overview β
Receiving fax, sending email β
- Receive the webhook from Telnyx indicating fax is incoming
- We only only care about the
fax.received
webhook forinbound
faxes - Extract the
to/from
and other information about the fax - Download the attachment and save locally
- Lookup the association between phone_number and email
- Create and send an email via Mailgun with downloaded media as an attachment
Sending fax β
- Receive webhook from mailgun for incoming email
- Extract the prefix of the email
(19198675309@MAILGUN_DOMAIN.com
, we want19198675309)
and prepend the + - Lookup the association between email and phone_number to determine the
from
phone number. - Save the first attachment locally
- Upload the attachment to S3
- Create and send a fax to the phone number extracted above
Update "DB" β
The app as provided uses a dumb-hard-coded database to minimize dependencies for the sake of examples. There is an in memory database defined at the top that associates an email to a Telnyx number.
DB = [
{
"email": "@telnyx.com",
"phone_number": "+"
}
]
phone_number
is a Telnyx phone numberemail
is the email to associate with that phone number
Receiving faxes β
{+19198675309} ==(faxes)==> {telnyx_phone_number} ==(emails)==> {email_as_defined}
Sending faxes β
{email_as_defined} ==(emails)==> {destination_phone_number@MAILGUN_DOMAIN} ==(faxes)==> {destination_phone_number}
Launch the app and update callbacks β
Start the server python app.py
When you are able to run the server locally, the final step involves making your application accessible from the Internet. So far, we've set up a local web server. This is typically not accessible from the public internet, making testing inbound requests to web applications difficult.
The best workaround is a tunneling service. They come with client software that runs on your computer and opens an outgoing permanent connection to a publicly available server in a data center. Then, they assign a public URL (typically on a random or custom subdomain) on that server to your account. The public server acts as a proxy that accepts incoming connections to your URL, forwards (tunnels) them through the already established connection and sends them to the local web server as if they originated from the same machine. The most popular tunneling tool is ngrok
.
Check out the ngrok setup walkthrough to set it up on your computer and start receiving webhooks from inbound faxes to your newly created application.
Once you've set up ngrok
or another tunneling service you can add the public proxy URL to your Inbound Settings in the Mission Control Portal. To do this, click the edit symbol [β] next to your Fax Profile. In the "Inbound Settings" > "Webhook URL" field, paste the forwarding address from ngrok into the Webhook URL field. Add faxes
to the end of the URL to direct the request to the webhook endpoint in your server.
For now you'll leave βFailover URLβ blank, but if you'd like to have Telnyx resend the webhook in the case where sending to the Webhook URL fails, you can specify an alternate address in this field.
Once everything is setup, you should now be able to:
- Fax your phone number and receive an email
- Email
{19198675309}@domain.com
an attachment to send a fax to{19198675309}
Fax follow-Ons β
Now that you've successfully sent a fax to your email and phone number, check out our on-demand webinar to learn more about fax and discover some ideas to build new features.
Our developer Slack community is full of Python developers like you - be sure to join to see what your fellow developers are building!