Send SMS Text Message with PHP

The following example PHP script, sendsms.php, can be used to send an SMS text message via NowSMS with PHP. This script can also be downloaded from http://www.nowsms.com/download/sendsms-php.txt.

A PHP script for sending MMS messages can be found at http://www.nowsms.com/doc/submitting-mms-messages/send-mms-message-with-php.

<?php
 
function SendSMS ($host, $port, $username, $password, $phoneNoRecip, $msgText) {
 
/* Parameters:
   $host - IP address or host name of the NowSMS server
   $port - "Port number for the web interface" of the NowSMS Server
   $username - "SMS Users" account on the NowSMS server
   $password - Password defined for the "SMS Users" account on the NowSMS Server
   $phoneNoRecip - One or more phone numbers (comma delimited) to receive the text message
   $msgText - Text of the message
*/
 
   $fp = fsockopen($host, $port, $errno, $errstr);
   if (!$fp) {
      echo "errno: $errno \n";
      echo "errstr: $errstr\n";
      return $result;
   }
   fwrite($fp, "GET /?Phone=" . rawurlencode($phoneNoRecip) . "&Text=" .
    rawurlencode($msgText) . " HTTP/1.0\n");
   if ($username != "") {
      $auth = $username . ":" . $password;
      $auth = base64_encode($auth);
      fwrite($fp, "Authorization: Basic " . $auth . "\n");
   }
 
   fwrite($fp, "\n");
   $res = "";
   while(!feof($fp)) {
      $res .= fread($fp,1);
   }
   fclose($fp);
 
   return $res;
 
}

/* This code provides an example of how you would call the SendSMS function from within
   a PHP script to send a message.
   The response from the NowSMS server is echoed back from the script.
   $x   = SendSMS("127.0.0.1", 8800, "username", "password", "+44999999999", "Test Message");
   echo $x;
*/
 
?>

The SendSMS function is the important part of the example. This is the function that needs to be included in your PHP script. You call this function, specifying the host name or IP address and port number of the NowSMS server, along with a username and password for an “SMS Users” account on the NowSMS server, plus the recipient phone number and text of the SMS message.

The SendSMS function uses these parameters to build a URL for connecting to the NowSMS server. This function could be easily modified to support sending other types of messages by modifying the URL that the function creates. For additional information on NowSMS URL parameters, see Submitting SMS Messages – URL Parameters.

As an additional example, here is how a web form might call a PHP script that uses sendsms.php to send an SMS message.

Below is a simple HTML web form that posts parameters to a PHP script named sendsmsscript.php:

<HTML>
<HEAD><TITLE>Send SMS</TITLE></HEAD>
<BODY>
<form method="post" action="sendsmsscript.php">
<table border="1">
<tr>
<td>Mobile Number:</td>
<td><input type="text" name="phone" size="40"></td>
</tr>
<tr>
<td valign="top">Text Message:</td>
<td><textarea name="text" cols="80" rows="10"></textarea>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Send">
</td>
</tr>
</table>
</form>
</BODY>
</HTML>

And here is an example sendsmsscript.php that processes the above form, validates the form parameters, and calls the SendSMS function from sendsms.php to submit the message:

if (isset($_REQUEST['phone'])) {
   if (isset($_REQUEST['text'])) {
      $x = SendSMS("127.0.0.1", 8800, "username", "password", $_REQUEST['phone'], 
                    $_REQUEST['text']);
      echo $x;
   }
   else {
      echo "ERROR : Message not sent -- Text parameter is missing!\r\n";
   }
}
else {
   echo "ERROR : Message not sent -- Phone parameter is missing!\r\n";
}



System Requirements for using this script:

This script connects to a NowSMS server and posts a request to the NowSMS server to send a message.

If you do not have a NowSMS server installed, this script will not work.

NowSMS server software is installed on a Windows PC and to be able to send SMS or MMS messages, NowSMS also requires a GSM modem, Android phone, or SMS service provider connection.

Free 30-day trial versions of the NowSMS software can be downloaded at http://www.nowsms.com/download-free-trial

Either the Now SMS/MMS Gateway or NowSMS Lite can be used. NowSMS Lite can send SMS and MMS messages using a single GSM modem or Android phone as a modem connection. The full product supports multiple modems and/or service provider connections.

Before attempting to interface with NowSMS using scripts, you should verify that NowSMS is configured correctly and can send SMS messages using its built in web interface. For Android phone or GSM modem connections, there are quick start guides that can be found here: http://www.nowsms.com/doc/quick-start-guide

Once you have verified that NowSMS is working on its own, it will be necessary to change IP addresses referenced in the script to point to your NowSMS server. Our sample scripts use an IP address of 127.0.0.1, which is a special loopback address for the current PC. If your script is running on a different system than the NowSMS server, change 127.0.0.1 to a host name or IP address that is valid for your environment. It is also necessary to define an SMS user account on the NowSMS server with account credentials that match with the user account in the script.