Get Started

Twilio SMS Quickstart

Sending SMS Messages during a phone call

We've responded to incoming SMS messages directly, but what about sending SMS during phone calls? Now we will send a message to anyone who calls our Twilio phone number. If you haven't yet, read over the TwiML Quickstart, as this example utilizes concepts introduced in that guide.

Next, set the voice URL of your Twilio phone number to http://companyfoo.com/sms-twiml.php where companyfoo.com is the name of your server and you have placed the sms-twiml.php file on your server.

  • quickstart/sms/sms-twiml.php
    <?php
    
    	// make an associative array of callers we know, indexed by phone number
    	$people = array(
    		"+14158675309"=>"Curious George",
    		"+14158675310"=>"Boots",
    		"+14158675311"=>"Virgil"
    	);
    
    	// if the caller is known, then greet them by name
    	// otherwise, consider them just another monkey
    	if(!$name = $people[$_REQUEST['From']]) {
    		$name = "Monkey";
    	}
    
    	// now greet the caller
    	header("content-type: text/xml");
    	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    <Response>
        <Say>Hello <?php echo $name ?>.</Say>
        <Sms><?php echo $name ?>, thanks for the call!</Sms>
    </Response>
        

When someone calls your Twilio phone number, the caller will be greeted by name and then sent an SMS message thanking them for calling. Isn't that nice?

For more information, see the <Sms> verb documentation.

And that's all she wrote folks! Back to Quickstarts.