Forums: How to forward Twilio SMS messages to your inbox - Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to forward Twilio SMS messages to your inbox

#1 User is offline   admin 

  • Administrator
  • PipPipPip
  • Group: Admin
  • Posts: 16322
  • Joined: 03-February 03
  • Gender:Male

Posted 05 February 2019 - 11:16 PM

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.c...t/sms2email.php

4. Proceed to Twilio control panel and update your Twilio Phone Number's webhook with this file's URL:
https://support.twil...ve-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

Best regards,
Sergey Kornilov
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users