How to Send a Free SMS Using PHP

By Chris Davis

PHP can send SMS messages from a computer to a mobile phone.
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: [email protected]";

?>

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: [email protected]";

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

?>

×