Forward Incoming SMS to email with Twilio Functions and Gmail
Time to read: 3 minutes
Forward Incoming SMS to email with Twilio Functions and Gmail
Ahoy builders! Forwarding an SMS to your email inbox is a great way to keep a track of all of your SMS conversations.
You can forward any incoming SMS message received by your Twilio number, directly to your email inbox! In this post, I’ll show you how to forward messages you receive using Twilio Programmable SMS to your Gmail inbox. How great is that?
However, before we get started, there are some small prerequisites you must have for this to work. I have broken these down for you in bullet points, below.
Prerequisites
This tutorial will require you to have a Twilio account with SMS capabilities, along with a Gmail inbox. These links will let you set up accounts, if you don’t have them yet.
Let’s start
You can purchase a Twilio phone number inside your Twilio account. If you are new to Twilio and this is your first account with us, you will be given a trial balance.
Once you have created a Twilio Account, we will use this to send and receive an SMS. You will need to purchase a Twilio Phone number with SMS capabilities.
Create an app password on your Gmail Account
To make this work, you will require an app password to be set on your Gmail Account. This will allow you to forward all your incoming SMS to your email address. App passwords are required when you have enabled 2 step verification on your Google Account.
To generate a Gmail App Password, you will need to navigate to Your Google Account > Security > 2 step Verification > App passwords or you can use this link to create App Passwords.
Give a name to your app, then click on Create.
After clicking on Create, you will get a 16 digit app password:
You need to use your app password without spaces, for example in this format: lfxfatsjfgdvynmc.
Set up your Twilio Function
Open up your Twilio console and create your first Function Service . Click on the blue Create service button of the Function Service page and enter forward-sms-email as the Service Name and click Next.
Once your Functions Service has been created, click on the Environment variables tab on the bottom left and create these two environment variables:
- EMAIL - This will be your Gmail address.
- PASS - This will be your app password you created in the previous section. Ensure you don’t leave the spaces in the password.
Once created, your Environment Variables section will look like the following:
Next, you’ll need to add the necessary dependencies needed for the Functions Service. Click on Dependencies and enter nodemailer as the Module and latest as the Version .
Next, you’ll create a Function for your service. Click on Add Function and give it a name, e.g., forward-sms-email:
After creating the function, replace the existing boilerplate code with the following code:
Hit Save followed by the Deploy All button to deploy and set your Function live. Give it a couple of seconds, and in the Live logs, you should see a message stating “Build completed”.
Once you see the “Build completed” message, your Function is now live and ready to go.
Configure your Twilio number to handle incoming SMS
Now that you have your function done, let's log in to the Twilio Console's Phone Numbers page. Once there, click on the Twilio phone number you'd like to have connected to your function.
- Find the A Message Comes In option under Messaging Configuration .
- Select Function from the A Message Comes In dropdown.
- Select the Service that you are using, then the Environment (this will default to ui, unless you have created custom domains)
- Select the Function Path of your Function from the respective dropdown menu.
- Click on Save configuration
After configuring your Twilio number, start sending SMS messages to your Twilio number. The function will automatically forward all the inbound messages to your email address.
If a message cannot go through, is delayed, or otherwise behaves unexpectedly, you can use our debugging tools to check the issue.
Conclusion
I hope that you’ve enjoyed this article and learned something new that could help your current or next project! If you’re curious what else you can build with Twilio Functions, I encourage you to check out some more examples in our docs.
We can’t wait to see what you build!
Sachin Kumar is a coding enthusiast who loves experimenting with APIs. He works as an emerging products support engineer at Twilio, helping other Twilio users in solving their API problems. He can be reached at sackumar [at] twilio.com.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.