如何从PHP中的mail()函数发送短信?

Here is my code in php for sending sms

   if(isset($_POST['frmType'])&& $_POST['frmType'] == "guest_mail"){
        $from = $_POST['from'];
        $to = $_POST['to'];
        $carrier = $_POST['carrier'];
        $message = stripslashes($_POST['message']);

        if ((empty($from)) || (empty($to)) || (empty($message))) {
        echo"<br/>".$from;
        echo"<br/>".$to;
        echo"<br/>".$message;
        }

        else if ($carrier == "Verizon" || $carrier == "verizon") {
        $formatted_number = $to."@vtext.com";
        mail("$formatted_number", "", "$message");

        echo"<script type=text/javascript>abc();</script>"; 
        }

        else if ($carrier == "T-Mobile" || $carrier == "tmobile" || $carrier == "Tmobile") {
        $formatted_number = $to."@tomomail.net";
        mail("$formatted_number", "", "$message");

        echo"<script type=text/javascript>abc();</script>"; 
        }
    }

Instead of mail function i have used 5 type of script for mail function

1. mail('$formatted_number','','$message');

2. mail('$formatted_number','','$message','$from');

3. mail('$formatted_number','$subject','$message','$from');

4. mail('$formatted_number','$subject','$message');

5. mail("$formatted_number", "SMS", "$message"); 

But still my SMS is not deliver to my brother

I refer the following link for sending the sms

http://www.ehow.com/how_7300418_send-sms-using-php.html

http://www.daniweb.com/web-development/php/code/291287/send-text-messages-with-php

http://www.textmessagetool.com/php_sms_script.php

But still it's not working.

I don't understand what exactly problem is?

Please help

Thanks in advance

To send SMS you need carriers. For that you need to register with some third party API provider's website, so they will provide an API. Using that API you can send the SMS.

If you want to send SMS using PHP mail function then you need to have address for the network’s SMS Gateway.

phone_umber(unique)@domainname.com 

Based on the mobile network the domain name will change. There are lists of SMS gateways.

Say example, you want to send a text to Mr. X, you could simply add 3855550168@vtext.com 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.

By The below script my SMS is successfully send

$message = 'Hello';

$to = XXXXXXXXXX(Unique phone number);

$formatted_number = $to."@domainname";

mail("$formatted_number", " ", "$message");

There is problem with the mail function.

For sending SMS the subject field of mail function should be blank.

My SMS is successfully delivered to my brother.

Actually, you CAN send with PHP mail(). It's a little tricky, and you HAVE to use headers instead of just a "from" address. It's better to have an SMS provider, but this method works.

Code to send mail:

$sms_gateways = file_get_contents('sms_gateways.txt'); //SMS gateways (see txt file below)

$tmp = split("
", $sms_gateways); //parse gateways file
$SMS_GATEWAYS = array();

for ($i=0; $i<count($tmp); $i++) {
    $itm = split(",", $tmp[$i]); //parse title/format
    $SMS_GATEWAYS[$itm[0]] = $itm[1]; //add # format to array
}

$eol = "
"; //varies between Linux/Mac/Windows End Of Line...usually 

$headers = 'From: SMS <sms@mywebsite.com>'.$eol;
$headers .= 'Reply-To: SMS <sms@mywebsite.com>'.$eol;
$headers .= 'Return-Path: SMS <sms@mywebsite.com>'.$eol;   
$headers .= "Message-ID:<".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$mime_boundary=md5(time());
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: text/plain; boundary=\"".$mime_boundary."\"".$eol; 

$phone = '8005551212'; //target phone # (do not use any symbols or spaces)

//get A/B/C pieces in phone #:
$phone_a = substr($phone, 0, 3);
$phone_b = substr($phone, 3, 3);
$phone_c = substr($phone, 6, 4);

//assemble gateways emails:

foreach ($SMS_GATEWAYS as $key => $val) {
  $email_str = str_replace("#a", $phone_a, str_replace("#b", $phone_b, str_replace("#c", $phone_c, $val)));
  if (mail($email_str, "", $sms_txt, $headers)) {
    //echo "success sending to $email_str<br>
";
    $sms_success_count++;
  } else {
    $sms_fail_count++;
  }
}

sms_gateways.txt file:

Alaska Communications Systems,#a#b#c@msg.acsalaska.com
Alltell Wireless,#a#b#c@message.alltel.com
AT&T Wireless (1),#a#b#c@txt.att.net
Bell Canada,#a#b#c@txt.bell.ca
Boost Mobile,#a#b#c@myboostmobile.com
Cellular One (Dobson),#a#b#c@mobile.celloneusa.com
Cellular South,#a#b#c@csouth1.com
Centennial Wireless,#a#b#c@cwemail.com
Cincinnati Bell,#a#b#c@gocbw.com
Cingular (Postpaid),#a#b#c@cingular.com
Cingular (Prepaid),#a#b#c@cingulartext.com
Cricket,#a#b#c@sms.mycricket.com
Koodo Mobile and Telus Mobnility,#a#b#c@msg.telus.com
MetroPCS,#a#b#c@mymetropcs.com
MTS Mobility,#a#b#c@text.mtsmobility.com
Nextel,#a#b#c@messaging.nextel.com
Pioneer Cellular,#a-#b-#c@zsend.com
Pocket Wireless,#a#b#c@sms.pocket.com
PC Telecom,#a#b#c@mobiletxt.ca
Qwest Wireless,#a#b#c@qwestmp.com
Rogers Wireless,#a#b#c@pcs.rogers.com
SaskTel,#a#b#c@sms.sasktel.com
South Central Communications,#a#b#c@rinasms.com
Sprint(PCS),#a#b#c@messaging.sprintpcs.com
Sprint(Nextel),#a#b#c@page.nextel.com
Straight Talk,#a#b#c@vtext.com
Syringa Wireless,#a#b#c@rinasms.com
T-Mobile (USA),#a#b#c@tmomail.net
Unicel,#a#b#c@utext.com
US Cellular (USA),#a#b#c@email.uscc.net
Verizon (USA),#a#b#c@vtext.com
Viaero (USA),#a#b#c@viaerosms.com
Virgin Mobile (CA),#a#b#c@vmobile.ca
Virgin Mobile (USA),#a#b#c@vmobl.com

This method sends an email to EVERY carrier in the list, but only one of them will actually receive it (since no two carriers use the same phone number.) This -could- potentially put your server on a SPAM blacklist, but if used only periodically it shouldn't cause a problem.

Further note: the reason I use #a#b#c in the gateways text file is because some gateways have a different format for the area code/prefix/suffix, ie: "#a-#b-#c@whatever.com"