How to Send a Free SMS Using PHP

Written by chris davis
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Send a Free SMS Using PHP
PHP can send SMS messages from a computer to a mobile phone. (fotohandy image by Marcus Scholz from Fotolia.com)

Short message service (SMS) messages, also known as text messages, have become the preferred way of communication for many people. They offer e-mail'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 e-mail is, meaning that PHP Web applications can be made that send SMS messages to users for free.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 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'];

    ?>

  2. 2

    Append the carrier's e-mail 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;

    }

    ?>

  3. 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.";

    ?>

  4. 4

    Set the message's headers. You will need to set a "From" header. You can set it to a standard e-mail, 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: sms@yourdomain.com";

    ?>

  5. 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: sms@yourdomain.com";

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

    ?>

Tips and warnings

  • PHP's mail function returns a boolean value, so your script can check to see if the message was sent successfully.
  • 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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.