This topic is locked

How to forward Twilio SMS messages to your inbox

2/5/2019 6:16:56 PM
PHPRunner Tips and Tricks
admin

If you use Twilio to send and receive SMS messages you might be interested to know how to forward incoming messages to your email inbox and also save them in the database.

  1. Create a text file named sms2email.php and paste the following code there.

    Point to your email address instead of "your-email@example.com"

<?php

require_once("include/dbcommon.php");
header("Content-type: text/xml");

echo "<?xml version='1.0' encoding='UTF-8'?>";

echo "<Response></Response>";
$email = "your-email@example.com";

$subject = "Message from {$_REQUEST['From']} at {$_REQUEST['To']}";

$message = "You have received a message from {$_REQUEST['From']}. Body: {$_REQUEST['Body']}";
runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $message));
// save in the database
$data = array();

$data["TextDateTime"] = now();

$data["TextNumberExternal"] = $_REQUEST['From'];

$data["TextNumberInternal"] = $_REQUEST['To'];

$data["TextMessage"] = $_REQUEST['Body'];

DB::Insert("text", $data );
?>


2. Make sure that SMTP settings are configured in this project so email can be sent.
3. Upload this file to the same folder on the web site where your project is located. URL will be something like http://yourwebsite.com/project/sms2email.php
4. Proceed to Twilio control panel and update your Twilio Phone Number's webhook with this file's URL:

https://support.twilio.com/hc/en-us/articles/223136047-Configuring-Phone-Numbers-to-Receive-SMS-Messages
5. If you need to save incoming messages in the database create a table named 'text' with the following fields:

TextDateTime - datetime

TextNumberExternal - varchar

TextNumberInternal - varchar

TextMessage - varchar


If you don't need to save text messages in the database remove the code after

// save in the database