Can we send sms using php?

Can we send sms using php?

i fotohandy image by Marcus Scholz from <a href='http://www.fotolia.com'>Fotolia.com</a>

Short message service (SMS) messages, also known as text messages, have become the preferred way of communication for many people. They offer email's benefit of mass messaging, but also have the additional benefit of being received nearly instantly. What many people don't know is that SMS messages can actually be sent through HTTP in the same way that an email is, meaning that PHP Web applications can be made that send SMS messages to users for free.

Step 1

Select the phone number that the message will be sent to. This could be acquired from a database, file, or HTTP headers. For this example, we will assume it was sent through HTTP using the GET method.

$recipient = $_GET['pnumber'];

?>

Step 2

Append the carrier's email domain to the end of the number. This example uses only three possible carriers.

$recipient = $_GET['pnumber'];

switch($_GET['carrier']){

case "verizon":

$recipient .= "@vtext.com";

break;

case "att":

$recipient .= "@txt.att.net";

break;

case "tmobile":

$recipient .= "@tmomail.net";

break;

}

?>

Step 3

Set the body of the message. Remember that most mobile carriers only allow messages of 140 characters or fewer to be sent and received via SMS.

$recipient = $_GET['pnumber'];

switch($_GET['carrier']){

case "verizon":

$recipient .= "@vtext.com";

break;

case "att":

$recipient .= "@txt.att.net";

break;

case "tmobile":

$recipient .= "@tmomail.net";

break;

}

$body = "This SMS message was sent with PHP.";

?>

Step 4

Set the message's headers. You will need to set a "From" header. You can set it to a standard email, or to the number of your mobile device (as long as you append the proper domain to the end). Any other headers are optional and may not even be read by the carrier's server.

$recipient = $_GET['pnumber'];

switch($_GET['carrier']){

case "verizon":

$recipient .= "@vtext.com";

break;

case "att":

$recipient .= "@txt.att.net";

break;

case "tmobile":

$recipient .= "@tmomail.net";

break;

}

$body = "This SMS message was sent with PHP.";

$header = "From: ";

?>

Step 5

Call PHP's built-in mail function to send the message. Leave the second parameter blank, since SMS messages don't have a subject field.

$recipient = $_GET['pnumber'];

switch($_GET['carrier']){

case "verizon":

$recipient .= "@vtext.com";

break;

case "att":

$recipient .= "@txt.att.net";

break;

case "tmobile":

$recipient .= "@tmomail.net";

break;

}

$body = "This SMS message was sent with PHP.";

$header = "From: ";

mail($recipient,"",$body,$header);

?>

References

Tips

  • PHP's mail function returns a boolean value, so your script can check to see if the message was sent successfully.

Warnings

  • Users will need to provide both their phone number as well as their carrier before you will be able to send them SMS messages.
  • Mobile customers in some countries cannot receive messages sent this way. Be sure to send your users a confirmation message when they give you their data to ensure that they are properly receiving messages. In order to send SMS to these users, you will need to pay for an SMS gateway.

Writer Bio

Chris Davis has been writing and editing since 2001. He has written for Elinon Online and written and edited content for the Oaks Fellowship. Davis studied computer science at Texas A&M University-Commerce and is now working on a Bachelor of Arts in English at Southwestern Assemblies of God University.

PHP Developers!

Start by checking out the possible solutions on how to add SMS functionality to your website. Read the quick overview:

Send SMS using HTTP:

With PHP you can easily submit SMS messages using HTTP requests:

The Ozeki NG SMS Gateway can send incoming messages to your PHP website through HTTP GET and POST requests:

SMS from MySQL:

If your website has a MySQL database, check out the following information:

RECOMMENDED FOR YOU:


Video tutorial:

Can we send sms using php?
How to send SMS from PHP using HTTP

Download: php-sms-send-receive.zip

This guide gives information on how you can add SMS functionality to your website. The method I present uses HTTP communication between the SMS Gateway and your website. To send and receive SMS messages from a webpage you need to have scripting support enabled on your webserver. The scripting support can be PHP or ASP. The examples presented here are using PHP.

Can we send sms using php?

How to send SMS from a website?

The example I present here shows how to send an SMS message from your website by clicking on a button. The idea we use is to have an HTML form, that allows the website visitor to enter a phone number and the message text. This HTML form can be submitted by clicking on the "SEND" button. Once submitted the form variables will be processed by a PHP script. This PHP script will post the SMS message to the SMS Gateway (Figure 1). As you can see on Figure 1. The SMS Gateway and your webserver (your webserver is hosting your HTML Form and your PHP script) can be on two different computers. If you wish you can install the Ozeki SMS Gateway software and the webserver with the HTML Form and the PHP script to the same computer.

Can we send sms using php?
Figure 1 - Send SMS from a website

The first step in setting up this system, is to Install the Ozeki NG SMS Gateway to your computer and to verify, that you can send SMS messages from the gateway manually. After your SMS gateway is functioning you can create the HTML Form and the PHP script.

Create the HTML Form for sms sending

To get this solution working you need to save the sendsms.html file into the WWW directory of your webserver. The sendsms.html file will contain the HTML Form that can be used to get the recipient telephone number and the message text from the website visitors. Here is the source of the sendsms.html file:

C:\www\sendsms.html

<html>
 <body>
   <h2>My SMS form</h2>
   <form method=post action='<B>sendsms.php</B>'>
   <table border=0>
   <tr>
     <td>Recipient</td>
     <td><input type='text' name='<[email protected]</B>'></td>
   </tr>
   <tr>
     <td>Message</td>
     <td><textarea rows=4 cols=40 name='<B>message</B>'></textarea></td>
   </tr>
   <tr>
     <td> </td>
     <td><input type=submit name=submit value=Send></td>
   </tr>
   </table>
   </form>
 </body>
</html>

After you save this file, you can use a browser (Internet Explorer or Firefox) to open it (Figure 2). On the form you will seen an input box for the recipient field and the message text. The recipient field will take the telephone number (e.g. +4407776134588) and the message field will take the message text.

Can we send sms using php?
Figure 2 - Open the SMS form in your browser

Please note that the form action tag points to the sendsms.php file. This means that the data entered by the website visitor will be posted to the sendsms.php file.

Prepare your PHP SMS script

As you can see the form target in this HTML form is the sendsms.php. The sendsms.php script will get the recipient and the message fields from the form and will build a URL to send the SMS message. This URL will be used to pass the SMS message to the SMS gateway. The URL is invoked in the PHP script using the file command. To get this script working you should configure the $ozeki_url variable to point to the SMS gateway. If you have installed the SMS Gateway to the same computer where your webserver is you can leave this untouched, otherwise you should change the IP address in the URL. The source code of the script is here:

C:\www\sendsms.php

<?php

########################################################
# Login information for the SMS Gateway
########################################################

$ozeki_user = "admin";
$ozeki_password = "abc123";
$ozeki_url = "http://127.0.0.1:9501/api?";

########################################################
# Functions used to send the SMS message
########################################################
function httpRequest($url){
    $pattern = "/http...([0-9a-zA-Z-.]*).([0-9]*).(.*)/";
    preg_match($pattern,$url,$args);
    $in = "";
    $fp = fsockopen("$args[1]", $args[2], $errno, $errstr, 30);
    if (!$fp) {
       return("$errstr ($errno)");
    } else {
        $out = "GET /$args[3] HTTP/1.1\r\n";
        $out .= "Host: $args[1]:$args[2]\r\n";
        $out .= "User-agent: Ozeki PHP client\r\n";
        $out .= "Accept: */*\r\n";
        $out .= "Connection: Close\r\n\r\n";

        fwrite($fp, $out);
        while (!feof($fp)) {
           $in.=fgets($fp, 128);
        }
    }
    fclose($fp);
    return($in);
}

function ozekiSend($phone, $msg, $debug=false){
      global $ozeki_user,$ozeki_password,$ozeki_url;

      $url = 'username='.$ozeki_user;
      $url.= '&password='.$ozeki_password;
      $url.= '&action=sendmessage';
      $url.= '&messagetype=SMS:TEXT';
      $url.= '&recipient='.urlencode($phone);
      $url.= '&messagedata='.urlencode($msg);

      $urltouse =  $ozeki_url.$url;
      if ($debug) { echo "Request: <br>$urltouse<br><br>"; }

      //Open the URL to send the message
      $response = httpRequest($urltouse);
      if ($debug) {
           echo "Response: <br><pre>".
           str_replace(array("<",">"),array("&lt;","&gt;"),$response).
           "</pre><br>"; }

      return($response);
}

########################################################
# GET data from sendsms.html
########################################################

$phonenum = $_POST['recipient'];
$message = $_POST['message'];
$debug = true;

ozekiSend($phonenum,$message,$debug);

?>

Once you have both: sendsms.html and sendsms.php installed, you can submit an SMS to the gateway. On a successful send the SMS gateway will return the message reference number (Figure 3). This number can be used to track the message inside the SMS Gateway.

Can we send sms using php?
Figure 3 - Successful SMS sending

Useful information

In order to be able to use this PHP script, you need to have the Ozeki NG SMS Gateway software installed to your computer. After Ozeki NG SMS Gateway is installed and configured, change the value of the $ozeki_url variable in the above PHP script to reflect the IP address of your computer. In other words, if you install Ozeki NG SMS Gateway to the computer, that has an IP address of 101.102.103.104, you should change the http://127.0.0.1:9501/api? value to http://101.102.103.104:9501/api?. Note that the ip address 127.0.0.1 has been changed in the $ozeki_url variable.

More information

  • How to send SMS from a website using PHP and MySQL
  • How to send a business card (Vcard SMS) from PHP
  • How to receive SMS on a website using PHP API
  • Two way SMS solutions using PHP and HTTP API
  • How to send SMS from a website using PHP API
  • How to add SMS functionality to a website using PHP API
  • Send SMS from PHP using HTTP
  • FAQ of the Ozeki NG SMS Gateway

Can I send SMS with PHP?

To send an SMS through PHP to Mr. Example, you could simply add [email protected] to any email client, type a message, and hit send. This will send a text message to phone number +1 (385) 555-0168 on the Verizon Wireless Network. For example, I'll send a text message to myself using Gmail.

How create SMS API in PHP?

How to integrate Messente's SMS API in PHP.
Step 1: Create your Messente account. You'll need to signup to Messente to get started (no credit card details required). ... .
Step 2: Create a sender ID. Next, apply for a sender ID. ... .
Step 3: Create an API key..
Step 4: Install our PHP code library..

How send SMS from localhost to Mobile in PHP?

Step 1 - Open XAMPP Control Panel. ... .
Step 2 - Start Apache Server. ... .
Step 3 - Copy the PHP SMS example source code. ... .
Step 4 - Paste the source code to a text file. ... .
Step 5 - Save the document as PHP file. ... .
Step 6 - Open 'localhost' to run the example code. ... .
Step 7 - Check the send result in the Ozeki log..

How can I send SMS to multiple numbers in PHP?

In the console, navigate to Messaging > Services. If you don't have a messaging service to use, create one by clicking on the blue Create Messaging Service button. When the prompt loads, input "Send Bulk SMS" as the Messaging Service Name. Click on the blue Create button once you're done.