Forward Incoming SMS to email with Twilio Functions and Gmail

February 15, 2024
No Translation Text Exists for this Label Key and Language.
No Translation Text Exists for this Label Key and Language.
Paul Kamp
Twilion

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.

Note that you will need to fulfill the requirements to register and use the number of your choice before you can complete the tutorial. Anyone sending SMS/MMS messages over a 10 Digit Long code (10DLC) number from an application to the US must register for A2P 10DLC. Other jurisdictions have similar requirements.

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.

Most jurisdictions, including the United States, require you to fill out certain information before you can use a phone number. You won’t be able to complete this tutorial until your number is registered.Refer to our Getting Started with Phone Number Regulatory Compliance guide.
Active Numbers page on Twilio Console

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.

App Passwords page on Google account

After clicking on Create, you will get a 16 digit app password:

Sample app password being shown on google account page

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.

Functions service creation on Twilio Console

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:

Environment variables section on the Function service

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 .

Dependency page on function service

 

Next, you’ll create a Function for your service. Click on Add Function and give it a name, e.g., forward-sms-email:

Add function page

After creating the function, replace the existing boilerplate code with the following code:

exports.handler = function(context, event, callback) {

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: context.EMAIL,
pass: context.PASS
}
});

var mailOptions = {
from: 'Twilio'+ '<'+context.EMAIL+'>',
to: 'test@abc.com',
subject: 'SMS received from - ' + event.From,
text: event.Body
};

transporter.sendMail(mailOptions, function(error, info){
if (error) {
return callback(null, error);
} else {
return callback(null, "Email Sent");
}
});
};

Replace test@abc.com in the above code with your actual email address where you want the incoming SMS message.

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”.

completed code on the function code editor

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
message configuration section within the twilio number page

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.