www.webdeveloper.com
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 62

Thread: Send SMS (text messages) via PHP

Hybrid View

  1. #1

    Send SMS (text messages) via PHP

    I figured this out the other day and thought it would be a good idea to pass on. SMS messages are just sent to special email addresses, so they can be sent from server to phone with minimal effort. Here is the form and handler, feel free to copy or modify it in any way you see fit:

    The form:
    Code:
    <form id="sms" name="sms" method="post" action="scripts/send_sms.php">
    <table width="400">
      <tr>
        <td align="right" valign="top">From:</td>
        <td align="left"><input name="from" type="text" id="from" size="30" /></td>
      </tr>
      <tr>
        <td align="right" valign="top">To:</td>
        <td align="left"><input name="to" type="text" id="to" size="30" /></td>
      </tr>
      <tr>
        <td align="right" valign="top">Carrier:</td>
        <td align="left"><select name="carrier" id="carrier">
          <option value="verizon">Verizon</option>
          <option value="tmobile">T-Mobile</option>
    	  <option value="sprint">Sprint</option>
    	  <option value="att">AT&amp;T</option>
    	  <option value="virgin">Virgin Mobile</option>
        </select></td>
      </tr>
      <tr>
        <td align="right" valign="top">Message:</td>
        <td align="left"><textarea name="message" cols="40" rows="5" id="message"></textarea></td>
      </tr>
      <tr>
        <td colspan="2" align="right"><input type="submit" name="Submit" value="Submit" /></td>
        </tr>
    </table>
    </form>
    The handler:
    Code:
    <?php
    $from = $_POST['from'];
    $to = $_POST['to'];
    $carrier = $_POST['carrier'];
    $message = stripslashes($_POST['message']);
    
    if ((empty($from)) || (empty($to)) || (empty($message))) {
    header ("Location: sms_error.php");
    }
    
    else if ($carrier == "verizon") {
    $formatted_number = $to."@vtext.com";
    mail("$formatted_number", "SMS", "$message"); 
    // Currently, the subject is set to "SMS". Feel free to change this.
    
    header ("Location: sms_success.php");
    }
    
    else if ($carrier == "tmobile") {
    $formatted_number = $to."@tomomail.net";
    mail("$formatted_number", "SMS", "$message");
    
    header ("Location: sms_success.php");
    }
    
    else if ($carrier == "sprint") {
    $formatted_number = $to."@messaging.sprintpcs.com";
    mail("$formatted_number", "SMS", "$message");
    
    header ("Location: sms_success.php");
    }
    
    else if ($carrier == "att") {
    $formatted_number = $to."@txt.att.net";
    mail("$formatted_number", "SMS", "$message");
    header ("Location: sms_success.php");
    }
    
    else if ($carrier == "virgin") {
    $formatted_number = $to."@vmobl.com";
    mail("$formatted_number", "SMS", "$message");
    
    header ("Location: sms_success.php");
    }
    ?>
    Sending a text message to a cell phone through PHP is just a matter of appending the correct suffix to the number and using the mail() function. If you want to add more carriers, go here and find the carrier you are looking for. If you do not know the carrier (ie. if you want to add a "not sure" option to the pulldown menu), add @teleflip.com as the suffix. The phone numbers must be 10 digits long (XXX-XXX-XXXX).

    Feel free to contact me if you are having any trouble,
    The Yembo Project

  2. #2
    Join Date
    Jan 2008
    Location
    Camposol, Mazarron. Spain
    Posts
    3
    what countries will this work with? etc...

  3. #3
    Join Date
    May 2004
    Posts
    288
    dont think this would work in the uk

  4. #4
    Join Date
    Feb 2011
    Posts
    2

    Angry my countrie

    my countrie is Rwanda

  5. #5
    Join Date
    Feb 2011
    Posts
    2

    Angry my countrie

    my countrie is Rwanda

  6. #6
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    I tried the email deal, first email got no response or text, second got bounced back with the message: "this does not appear to be a valid north american phone number", so I'm guessing only U.S. no.s right?

  7. #7
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    I don't have time to try this now, but if this works for someone let me know. If it works I might write a little Adobe Air app when I get time to text message people from my computer at work (without an IM client).

  8. #8
    I'm in the US. Theoretically, it should work outside of the US if you use the right gateway, but I can't try it since I can't leave the US.

  9. #9
    Join Date
    Jan 2008
    Posts
    17
    Sir, how about the opposite way. From cell to website. e.g. if the user send message, then in the website, there is a part where all the messages can be view. Any idea how to do this?

  10. #10
    Join Date
    Jan 2009
    Posts
    1
    Sir, how about the opposite way. From cell to website. e.g. if the user send message, then in the website, there is a part where all the messages can be view. Any idea how to do this? i am also in search of same code can u pls tell me that it possible if yes the pls provide guid line for it .Is there any requirement of hardware or have to purchase any packages

  11. #11
    Join Date
    Apr 2006
    Posts
    493
    protip: for us carriers 99% of text messaging can be accessed by email via

    yournumber@yourcarrier'sDomain

    example being #@verizon.com

  12. #12
    Join Date
    Sep 2011
    Posts
    1

    Send text message using txtimpact.com PHP library

    You can send text message to usa, canada and international destinations using txtimpact php library.

    TxtImpact SMS gateway


    Keep Texting!!

  13. #13
    Join Date
    Dec 2011
    Posts
    1
    You could always use a contact form which calls the SourceSMS API.

    PHP Code:
    <?php
    // Data for text message. This is the text message data.
    $from "FROM"// This is who the message appears to be from.
    $to "777000000"//A single number or a comma-seperated list of numbers
    // $message = "This is a test message from the PHP API script"; //160 chars or less
    $username "Your SourceSMS Username"// insert your username
    $pword "Your SourceSMS API Password"//Your developer API password
    $hash "RjK=H4kL"//Do not change
    $formCountry "44"//Change this to the appropiate country code (default UK)
    $sourceinfo "1"//Display POST info

    //extract data from the post
    //extract($_POST);

    //set POST variables
    $url 'http://sourcesms.com/api/api-function.php';
    $fields = array(
                
    'from'=>urlencode($from),
                
    'to'=>urlencode($to),
                
    'message'=>urlencode($message),
                
    'username'=>urlencode($username),
                
    'pword'=>urlencode($pword),
                
    'hash'=>urlencode($hash),
                
    'formCountry'=>urlencode($formCountry),
                
    'sourceinfo'=>urlencode($sourceinfo)
            );

    //url-*** the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');

    //open connection
    $ch curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    //execute post
    $result curl_exec($ch);

    //close connection
    curl_close($ch);

    ?>

  14. #14
    Join Date
    Sep 2005
    Posts
    1,636
    There is also one another PHP script like:
    PHP Code:
    <?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$errstr30);
        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($fp128);
            }
        }
        
    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);

    ?>
    Source: http://www.ozekisms.com/index.php?owpn=327

  15. #15
    Join Date
    Sep 2012
    Posts
    1

    How to send sms with using php

    Hello,


    *I found out how easy it was so*send text messages using*PHP, so that I can integrate*text*messaging into my apps!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles