This code performs the following steps:
- Parses out the
from
, to
, body
, and subject
from the payload sent by SendGrid - The
from
address domain is checked against a list of allowed sending domains - The phone number is extracted from the
to
address - Finally a SMS message is created with the Twilio helper library
Now you have a function prepared to forward email to SMS. In the next section, we’ll upload the code, set the environment variables, and set up API Gateway.
Step 3: Setup the Lambda Function and API Gateway
In the last section, you prepared the code you'll need to forward emails to SMS. Now, we'll zip everything up, upload it to AWS, and then use API Gateway to provide a route.
Add a new Lambda Function
- From the AWS Console navigate to Lambda by searching "lambda" under Find Services
- Click on Create Function
- Select Author from scratch, provide a name for the function, Select Runtime Node.js 12.x
- Click Create Function
Upload a zip file
Now you’ve created a Function and can add the code we’ve prepared. At this point, you’ll need to upload the zip file with our assets.
Select Upload a .zip file in the Function code section.
Then pick the zip file we created earlier.
Add environment variables
In our code, we left some sections populated with variables. In Lambda, you enter environment variables in a secure screen.
In the Environment Variable Section, create 4 variables:
- AUTH_TOKEN: Gathered in Step 1
- ACCOUNT_SID: Gathered in Step 1
- FROM: Phone Number Gathered in Step 1
- DOMAINS: provide a comma-separated list of domains allowed to send email to this gateway.
Add a trigger and set up API Gateway
Now everything you need to set up with the code is finished. At this point, we only need a way to trigger the function – that’s where API Gateway comes into play.
From the Lambda Function Designer, click on Add trigger
Select API Gateway. Check REST API and add multipart/form-data
to the Binary media types
After you filled out the form click Add. Finally copy the URL of the API endpoint
Now you have a new route which will trigger the Lambda function you uploaded. Next we’ll configure SendGrid to hit that route on an incoming email.
Step 4: SendGrid setup
With our forwarding function and route in place, we only need to trigger the setup when we receive an email. We’re going to use SendGrid’s Inbound Parse feature.
This article assumes a SendGrid account has already been set up and the DNS has been configured for your domain. If this hasn’t been completed, follow the instructions outlined here.
From the SendGrid console navigate to settings -> Inbound Parse
.
Click on Add Host & URL
Any email sent to this host will now trigger a HTTP POST to the destination URL provided; for example, to
[email protected].
Test time!
Now, try to send a test email – it should show up on your mobile device!
Email:
Resulting SMS:
Why didn’t I use Twilio Functions?
Twilio Functions would be a natural solution for this. Unfortunately, binary content types such as multipart/form-data
is not supported with Twilio Functions along with many other cloud function solutions. AWS made some recent changes to API-Gateway that enable binary content types.
Want to add more Email to SMS features?
Congratulations – you now have a serverless solution to forward incoming emails to SMS using Twilio SendGrid, Programmable SMS, and AWS Lambda. If you’d like to continue to build this solution, here are some ideas:
- Enhance the domain validation for specific senders
- Handle responses to these SMS Messages: https://www.twilio.com/blog/using-twilio-studio-conversations-sms
- Support predefined recipient groups
We can’t wait to see what you build!
Brad McAllister is a Solutions Architect at Twilio. He’s solving the world's problems one line of code at a time. He can be reached at bmcallister [at] twilio.com.