If you want to read incoming SMS messages sent to your Twilio number in your email then do I have a neat solution for you today. We’re going to use Twilio Functions and SendGrid to forward SMS messages directly to your email address.

Things you’ll need

A Twilio account and a phone number that can receive SMS messages

A SendGrid account

Let’s get building

The entire operation of forwarding SMS messages to your email address will only take one function, so we’re going to build it using a Twilio Function. If you’ve not heard of Twilio Functions, they are a serverless environment that you can use to run Node.js code. This means we don’t need a development environment or somewhere to deploy to, we just write our code in Twilio and use it.

We do need to configure some variables though.

First up, config

In order to use the SendGrid API to send emails we need a SendGrid API key. Head to the API Keys area in the SendGrid dashboard and create yourself one.

Open up your Twilio console and head to the Functions configure section. Add a new environment variable called SENDGRID_API_KEY and paste in your new API key.

We need to make two more environment variables too, one for the email you will use to receive your SMS messages and one to send them from. If you have a domain setup within SendGrid then I recommend you use that, but you can use any email address you want as the sender, even the one you are sending to.

Enter these email addresses in the environment variables section as TO_EMAIL_ADDRESS and FROM_EMAIL_ADDRESS .

We’re done with the config, it’s time to write some code.

Let’s go write a Function

Head to the Functions management page and create yourself a new function. Since we’re dealing with incoming SMS messages, pick the “Hello SMS” template.

Normally with Node.js this would be the time to start a search for the SendGrid npm package, but Functions don’t currently allow you to install other packages. Thankfully there is one package available to us that will help us make HTTP requests easily: got.

Update: Functions stopped including got by default but allows you to install dependencies from npm. To continue with this post you now need to add got to your dependencies. Open your Functions configuration page and scroll down to dependencies. Add got and the version 8.3.1 then save.