Source: Dev.to | Jan 30, 2026 (UTC)
TL;DR: On-call teams often face bottlenecks sharing 2FA codes from physical phones. This solution details building a robust Twilio-to-email forwarder using a Python Flask webhook to automatically deliver SMS codes to a team distribution list, eliminating coordination overhead.
- A Python Flask application serves as the webhook endpoint, receiving incoming SMS from Twilio and processing them for email forwarding.
- Environment variables, loaded via
python-dotenvfrom aconfig.envfile, are crucial for securely managing sensitive credentials like Twilio Account SID/Auth Token and email service API keys. - Proper configuration of the Twilio phone number’s ‘A MESSAGE COMES IN’ webhook URL to point to the publicly accessible Flask application endpoint (e.g.,
https://your-app-name.com/sms) is essential for functionality.
Forwarding SMS from Twilio to Email for 2FA Sharing
Darian Vance here. Let’s talk about a common headache in any team that shares on-call duties or access to a central service: the dreaded 2FA code. For a while, my team had a physical “on-call phone” that we’d pass around. It was a mess. The phone would get left at the office, run out of battery, you name it. We were constantly blocked. I finally decided to solve this properly, and this Twilio-to-email forwarder is the result. It’s a simple, robust solution that turns SMS codes into emails sent to a team distribution list. It saved us countless hours of coordination overhead, and I think it can do the same for you.
Let’s get this set up. It’s faster than you think.
Before we dive in, make sure you have the following ready to go:
- A Twilio account with an active phone number. You’ll need your Account SID and Auth Token.
- An email delivery service account. I’m a fan of SendGrid for its reliability, but others like Mailgun or AWS SES work just as well. You’ll need an API key.
- A Python environment. We’ll be using Flask to create a simple web server to act as our webhook.
The Guide: Building the Forwarder
Step 1: Set Up Your Project and Environment
First things first, get your project directory organized. I’ll skip the standard virtualenv setup since you likely have your own workflow for that. Just make sure you have a clean environment to work in. You’ll need to install a few Python packages. I use pip for this, so you’d run the commands to install twilio, flask, python-dotenv, and your email provider’s library (e.g., sendgrid).
Next, create a file named config.env. This is where we’ll store our secrets. Storing credentials directly in the code is a huge security risk, and this method keeps them separate and safe. Your file should look like this:
# Twilio Credentials TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx TWILIO_AUTH_TOKEN=your_twilio_auth_token # Email Configuration SENDGRID_API_KEY=SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx [email protected] [email protected]
Enter fullscreen mode
Exit fullscreen mode



Залишити відповідь