www.webdeveloper.com
Page 3 of 5 FirstFirst 12345 LastLast
Results 31 to 45 of 62

Thread: Send SMS (text messages) via PHP

  1. #31
    Join Date
    Jan 2009
    Posts
    4
    I am having a little trouble with the code and was wondering if I could get your help. It seems that I can't get the handler code to communicate with the form.

  2. #32
    marlowjust, send me your code and we'll have a look!

  3. #33
    Join Date
    Jan 2009
    Posts
    4
    I just used the same code you posted and made two files out of it. One a php file and the other html file. Then uploaded the two of them to my server. I'm more of a beginner working with php and such so I know I've done something wrong. Should I modify anything other than the email address gateway for phone providers? I can get everything to display properly but when I hit submit text nothing happens.

  4. #34
    marlowjust,

    Are you sure your form is submitting to the php script? On the first line of code I posted for the form, you'll see the action attribute being set:

    Code:
    action="scripts/send_sms.php"
    This is a relative link, so you'll need to make sure that relative to the file with the HTML form in it, the php script is names send_sms.php and in a folder called scripts. Alternatively, you could change the action attribute to point to the location of the php form handler file.

    If this doesn't help, just let me know. A URL would help too, if at all possible.

    Cheers,
    Zach
    Last edited by TheYemboProject; 01-31-2009 at 01:27 PM.

  5. #35
    Join Date
    Jan 2009
    Posts
    4
    Perfect, that worked and I can now text! I was also wondering...is there a way I make it so when the person texting on my page can enter their email address and it will show up to the person receiving the text? Right now a text says it is from name@server03-01.hostingcomplex.com

    I've always wanted to enable replies to a person's email address. I've seen some other texting sites do it but now sure what all that would involve. Thank you for your help.

  6. #36
    Sure, just set your own mail headers. Here's a basic example:

    Code:
    $Email = $_POST['Email'];
    // Collect other variables from POST here
    
    mail($PhoneNumber, $Subject, $Message, 'From: ' . $Email);
    For more information, check out the PHP manual for mail() at http://us2.php.net/manual/en/function.mail.php. Scroll down to the description for additional_headers to see all your options. I don't know if you've used the PHP manual in the past, but it is by far the best documentation I've found for the language. And it's free!

    Let me know if you need anything else,
    Zach

  7. #37
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by TheYemboProject View Post
    ...
    The handler:
    PHP 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");
    }
    ?>

    ...The Yembo Project
    Would be easier to do something like this.
    PHP Code:
    <?php
    $from 
    $_POST'from' ];
    $to $_POST'to' ];
    $carrier $_POST'carrier' ];
    $message stripslashes$_POST'message' ] );

    // are we empty? if not, continue
    if ( !empty( $from ) && !empty( $to ) && !empty( $message ) ) {
        
    // some carriers
        
    $carriers_info = array(
                    
    "verizon" => "vtext.com",
                    
    "tmobile" => "tomomail.net",
                    
    "sprint" => "messaging.sprintpcs.com",
                    
    "att" => "txt.att.net",
                    
    "virgin" => "vmobl.com"
                    
    );
        
        
    // test to see if the carrier exists in the array
        
    if( in_array$carrier $carriers_info ) ){
            
    // format the connection string
            
    $formatted_number $to "@" $carriers_info$carrier ];
            
    // send the message
            
    if( mail("$formatted_number""SMS""$message") ) header "Location: sms_success.php" );
        }
    // end of if - in_array

    }// end if - empty
    header "Location: sms_error.php" );
    ?>
    Any errors get caught like if the mail attempt fails.

    Certain hosts do not allow straight use of the mail send function, so take note, if you are unable to send then I suggest you consult your web host FAQ on this as each host will have their own method or process that you need to configure before you can send emails.

  8. #38
    Join Date
    Jan 2009
    Posts
    4
    When trying to modify the handler so that it will display the user's email address to the person they are texting (for replies) I always run into an error. Here is what I'm trying to add..

    Regular Handler
    PHP Code:
    <?php
    $from 
    $_POST'from' ];
    $to $_POST'to' ];
    $carrier $_POST'carrier' ];
    $message stripslashes$_POST'message' ] );

    // are we empty? if not, continue
    if ( !empty( $from ) && !empty( $to ) && !empty( $message ) ) {
        
    // some carriers
        
    $carriers_info = array(
                    
    "verizon" => "vtext.com",
                    
    "tmobile" => "tomomail.net",
                    
    "sprint" => "messaging.sprintpcs.com",
                    
    "att" => "txt.att.net",
                    
    "virgin" => "vmobl.com"
                    
    );
        
        
    // test to see if the carrier exists in the array
        
    if( in_array$carrier $carriers_info ) ){
            
    // format the connection string
            
    $formatted_number $to "@" $carriers_info$carrier ];
            
    // send the message
            
    if( mail("$formatted_number""SMS""$message") ) header "Location: sms_success.php" );
        }
    // end of if - in_array

    }// end if - empty
    header "Location: sms_error.php" );
    ?>
    A regular send message script that will text to a number and display an email address from the sender.
    PHP Code:
    <?php
      $email 
    $_REQUEST['email'] ;
      
    $message $_REQUEST['message'] ;

      
    mail"number@messaging.sprintpcs.com""Feedback Form Results",
        
    $message"From: $email);
      
    header"Location: http://www.example.com" );
    ?>
    The main thing I'm looking to do is add the request email function of the second script to the first script so it will request an email address and then display it in the "from" section of the text message. I've tried many times to mess with it and always get an error.

  9. #39
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    I wasn't paying much attention to detail, I just thought that as a routine it was easier to read and follow, anyway, theirs no difference, as long as the carrier is set then the resulting string will be made up of the additional parameters you include.

    If any of the values you try to aquire or set in variable names can result in an error if they are not set. ensure that your passing all the required info.

    The mail function is a bit short, what about adding & changing this instead.
    PHP Code:
    if ( !empty( $from ) && !empty( $to ) && !empty( $message ) && !empty( $carrier ) ) { 
    PHP Code:
    $headers "From: {$from}\r\nReply-To: {$from}\r\nX-Mailer: PHP/" phpversion();
    if( 
    mail$formatted_number"SMS"$message$headers) ) header "Location: sms_success.php" ); 
    You will also need to look up in your host FAQ on sending php mails, some do not allow, some do but with limits, some do not care and you can send as many as you like, check what your host required, one host I used did not allow the send mail function unless it had some other coding prior to and after the mail function as well as that the email address was forced via these external functions.

  10. #40
    Join Date
    Feb 2009
    Posts
    1
    Hi, thanks for the information it seems to work great. I wanted to know what script to use if a person want to send the same message to more than one person. Say send same message 2 5 people who have the same carrier.

    or is there some check box fields that can be embed as code. Thanks.

  11. #41
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Don't know, sorry.

    I would imagine something like, $recp would be text input boxes from a form but here I just use an array for demo.
    PHP Code:
    $recp = array("user1@domin.com","user33@domin.com","user21@domin.com","user313@domin.com");
    $snt=array();
    foreach(
    $recp as $key=>$sndmsg){
       
    $snt[$key]=mail$sndmsg"SMS"$message$headers);
    }
    $report = array("Your message was sent to:-<br>");
    foreach(
    $snt as $key=>$res) if($resarray_push($report,$recp[$key]."<br>");
    if(
    in_array($snt,false)){
      
    array_push($report,"The following were not sent:-<br>");
      foreach(
    $snt as $key=>$res) if(!$resarray_push($report,$recp[$key]."<br>");
      }
    $output implode("\r\n",$report);
    echo 
    $output
    ... or something like that

  12. #42
    Join Date
    May 2010
    Posts
    1
    Hi, I need to send SMS(text messages) to Users having US mobile numbers from my PHP application. I do not know the service provider of those numbers? Will U please help me?

  13. #43
    Join Date
    Sep 2010
    Posts
    1
    Sorry to revive an old thread but I have a question.

    I cannot get this script to accept my from address, rather it replaces my from address with se@p5skj233.shr.phx8.secureserver.net

    I am on a godaddy hosting plan, not sure how to fix this.

    Thanks,
    Stephen

  14. #44
    Join Date
    Mar 2009
    Posts
    452
    Plz let me know if someone has gateway(s) for Pakistan

  15. #45
    Join Date
    Dec 2010
    Posts
    1
    for sms sending you can use sms gateway, you can find some examples, one of them

    PHP Code:
    <?
    /*
     Simple and easy for modification, PHP script for SMS sending through HTTP with you own Sender ID and delivery reports. 
     You just have to type your account information on www.2-waysms.com and upload file on server.
    */
    ?>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>SMSER</title>
    <style type="text/css">
    body{
        font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; 
        font-size:12px;
    }
    p, h1, form, button{border:0; margin:0; padding:0;}
    .spacer{clear:both; height:1px;}
    /* ----------- My Form ----------- */
    .myform{
        margin:0 auto;
        width:300px;
        padding:14px;
    }
    /* ----------- stylized ----------- */
        #stylized{
            border:solid 2px #b7ddf2;
            background:#ebf4fb;
        }
        #stylized h1 {
            font-size:14px;
            font-weight:bold;
            margin-bottom:8px;
        }
        #stylized p{
            font-size:11px;
            color:#666666;
            margin-bottom:20px;
            border-bottom:solid 1px #b7ddf2;
            padding-bottom:10px;
        
    }
        </style> 
    </head>

    <body>
    <?

        
    switch ($option) {

        case 
    sendsms:
            if (
    $text == "") { echo "Error!<br>Text not entered<br><a href=\"javascript:history.back(-1)\">Go Back</a>"; die; } else { }
            if (
    $to == "") { echo "Error!<br>Number not entered<br><a href=\"javascript:history.back(-1)\">Go Back</a>"; die; } else { }

            
    $url "http://www.2-waysms.com/sms.php";
            
    $postfields = array ("text" => "$text"// do not need to change
            
    "to" => "$to"// do not need to change
            
    "id" => "*****"// you ID in www.2-waysms.com accout
            
    "sec" => "****************************"); // your secret code in www.2-waysms.com account
            
    if (!$curld curl_init()) {
            echo 
    "Could not initialize cURL session.";
            exit;
            }
            
    curl_setopt($curldCURLOPT_POSTtrue);
            
    curl_setopt($curldCURLOPT_POSTFIELDS$postfields);
            
    curl_setopt($curldCURLOPT_URL$url);
            
    curl_setopt($curldCURLOPT_RETURNTRANSFERtrue);
            
    $output curl_exec($curld);
            
    curl_close ($curld);
            
    $out explode('|',$output);

            echo 
    "Message Status: $out[1]"

            
    //Header("Location: $PHP_SELF");
        
    break;

        default:

        echo 
    "<div id=\"stylized\" class=\"myform\">"
           
    ."<h1>Send Sms</h1>"
           
    ."<form method=post action=\"$PHP_SELF?option=sendsms\">"
           
    ."<table border=\"0\">"
           
    ."<tr>"
             
    ."<td>Number</td>"
             
    ."<td><input type=\"text\" name=\"to\"></td>"
           
    ."</tr>"
           
    ."<tr>"
             
    ."<td>Message</td>"
             
    ."<td><textarea rows=\"4\" cols=\"25\" name=\"text\"></textarea></td>"
           
    ."</tr>"
           
    ."<tr>"
             
    ."<td>&nbsp;</td>"
             
    ."<td><input type=submit name=submit value=Send>"
             
    ."<div class=\"spacer\"></div></td>"
           
    ."</tr>"
           
    ."</table>"
           
    ."</form>"
        
    ."</div>";


        }

    ?>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 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