How to Send a Free SMS Using PHP
By Chris Davis
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);
?>
References
Tips
- PHP's mail function returns a boolean value, so your script can check to see if the message was sent successfully.
Warnings
- 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.
Writer Bio
Chris Davis has been writing and editing since 2001. He has written for Elinon Online and written and edited content for the Oaks Fellowship. Davis studied computer science at Texas A&M University-Commerce and is now working on a Bachelor of Arts in English at Southwestern Assemblies of God University.