29 lượt xem

Build Two-Way Email to Text Forwarding with SendGrid and Twilio

  1. Copy the deployment address shown.

Step 3: Twilio setup

Configuring the phone number

To make sure inbound text messages are sent as emails, we’ll need to configure our phone number to point to our function. To do this, select your phone number from the Active Numbers page and update the messaging section A Message Comes in to Webhook and paste in

<deployment url>/api/sms2email that is add /api/sms2email to the end of your deployment url in the webhook section as shown below.

Setting a messaging webhook with Twilio

Step 4: SendGrid setup

Receiving email – AKA Inbound Parse

To receive emails, we need to configure an Inbound Parse Webhook. When this is set, SendGrid will parse an email and its attachments and POST it to a URL specified. In our case, we’re going to specify a function URL so we can process the inbound emails. To process inbound emails, we need to specify a hostname dedicated to parsing inbound emails such as parse.<yourdomain>.com and update the MX records for this hostname to point to mx.sendgrid.net. This sounds complicated, but fortunately, the documentation we’ve provided has you covered. You set the receiving domain to what you picked above and point the destination URL to <deployment url>/api/email2sms

Add an inbound parse webhook with SendGrid

Step 5: Testing SMS and email forwarding

To test out your deployment, first send an SMS to the Twilio phone number you used. In less than a minute, you should receive an email to the email address you specified above. You can reply directly to this email, and shortly after, you should receive an SMS back from the original phone number. You can also send an SMS by emailing <e.164 phone number>@<email domain> where an E.164 number is the country code plus the phone number without any leading zeros.


Hopefully, everything works without a hitch. But if there are any problems, head to your Zeit Dashboard, click on the deployment name, and view the logs. Any deployment errors and everything logged to the console should show up here. Additionally, you can check the Twilio Debugger, which shows any problems on the Twilio side with instructions on how to resolve them.

Forward SMS to email (and email to SMS) using Twilio

The solution shown here is a simple two-way link between a shared inbox and a Twilio phone number. You can extend this to support multiple inbox-to-phone-number mappings by leveraging an array or database to map between a Twilio phone number and the email address of choice.

Learn more about what you can do with Twilio’s SMS API.

Toby Allen is a Twilio Solutions Architect working with customers large and small in Australia. He helps introduce the world’s best Communications Cloud downunder, one CURL request at a time. He is helping companies build great user experiences making sure that they leverage all the contextual information they have at hand. He has been working in real time communications for 15 years from developing video solutions to enterprise to helping carriers deploy globe spanning voice and messaging solutions including more than two years working with communications clouds. He’s active on Twitter @tobyallen and LinkedIn.