Can I forward SMS to an email using ONLY a twiml bin?
Asked Answered
D

2

5

It's very clear how you should forward an SMS to an email address using twilio - they have a good example of doing it with php code hosted on a third party server.

However, I would like to forward an SMS to an email address using ONLY a twiml app and no other third party request / code. I attempted this using the function:

<Response>
<Message to="[email protected]">{{From}}: {{Body}}</Message>
</Response>

... but this did not work, presumably because only accepts a phone number as the "to:".

Is it possible to do this using only a twiml bin ?

Dense answered 26/6, 2017 at 21:8 Comment(0)
Z
6

OK you said no 3rd party request/code, but how about a request to Google and your own code?

If you have a Gmail account you can do it with Google Scripts. They host the code for you and it's pretty easy.

Go to https://script.google.com and create a new project, delete the code in the window and paste in the following, replacing the email address with the one you want to get the emails sent to.

function doGet(e){
 var from = e.parameter.From;
 var to = e.parameter.To;
 var body = e.parameter.Body;
 var emailAddress = '[email protected]';
 var message = body;
 var subject = 'SMS from ' + from + ' to ' + to + ' received';
 MailApp.sendEmail(emailAddress, subject, message);
 var output = ContentService.createTextOutput('<Response/>');
 output.setMimeType(ContentService.MimeType.XML);
 return output;
}

From the Publish menu choose deploy as web app. Set Execute the app as: to Me, then set Who has access to the app: to Anyone, even anonymous.

Now click deploy and Google will ask you to authenticate your script to allow it to send email as you (but not access your incoming email). Once you have granted permission it will give you the URL for your script.

Paste this URL into your Twilio console as the webhook handler for when a message comes in and set the method to HTTP GET

All this will do is email you the SMS. Change the <Response/> in the line below to contain TwiML for replying to the sender or whatever else you want Twilio to do when messages come in.

var output = ContentService.createTextOutput('<Response/>');

Obviously this is just a basic example, there's no authentication/security etc so any call to your script URL will send you an email. You should really secure it, although the URL Google generates isn't the kind anyone is likely to stumble upon.

Zsa answered 27/6, 2017 at 22:34 Comment(1)
Thank you very much - this did indeed work. You need to: Publish -> deploy as web app before it will work, and you need to grant "anonymous" access to run it.Dense
P
1

Twilio developer evangelist here.

You can't forward SMS messages to an email address with just a TwiML Bin. You are right, the <Message> TwiML does only accept a phone number in the to attribute.

This could be done using a Twilio Function and some Node.js code. I haven't done that before so would have to spend some time building it in order to tell you how.

There is an example here of how to build an app that forwards SMS to email using PHP.

Alternatively, you could set up Zapier to forward SMS messages to your email.

Let me know if that helps at all.

Picasso answered 27/6, 2017 at 11:1 Comment(3)
Thanks very much. Yes, I suppose this is a good use for the new function features ... would the node.js code need to be hosted somewhere or can you put everything into the function and host it all at twilio ?Dense
Everything could go in the function and be hosted at Twilio. That's why we built Functions!Picasso
The Zapier integration for Twilio is significantly out of date. It uses 5 min polling rather than webhooks as the trigger.. so for any 2FA use it doesn't work as the sms to email is too delayed.Metamathematics

© 2022 - 2024 — McMap. All rights reserved.