October 30, 2011 in PHP, Programming, Tutorials by admin

You have probably seen the plenty of paid services online that allow you to send out text messages (SMS) from a website, but did you know you could send them for free and easily with PHP? In this tutorial you will find out how to send out your very own text messages with PHP. You can even try it yourself live within minutes!

Using the PHP mail() function we can actually e-mail text messages to cellular phones using a web server. If you are unfamiliar with the mail() function in PHP here is a basic example of how to send an email.

1
2
3
4
5
6
7
<?php
    $recipient = 'email@example.com';
    $subject = 'your sample subject';
    $message = 'your sample message';
    mail($recipient, $subject, $message);
?>

All you have to do to send an e-mail with PHP is define a recipient, a subject, and a message. Simple enough right? Sending an SMS message with PHP is just as simple!

Almost every cellular carrier actually has an e-mail address domain in which you can send an e-mail message to someone’s cellphone so long as they are using that carrier. For example Verizon Wireless uses @vtext.com to send sms text messages. Here is a simple example on how to send an sms text message to a verizon phone number using the same PHP code as above with only a change to the recipient.

1
2
3
4
5
6
7
<?php
    $recipient = '5559997777@vtext.com';
    $subject = 'your sample subject';
    $message = 'your sample message';
    mail($recipient, $subject, $message);
?>

That is it! It really is that easy and all you have to do is know the cell phone carrier of the phone number. There are a lot of carriers out there and new ones are popping up often so you will need to remember to update your email carrier lists. Below I have listed the e-mail addresses for a few major cell phone carriers.

1
2
3
4
5
6
<?php
    //Verizon    @vtext.com
    //T-Mobile    @tmomail.net
    //AT&T        @txt.att.net
    //Sprint    @messaging.sprintpcs.com
?>

You could even expand on this to have it be more automated. You could even use this and detect the cellular carrier just by the phone number. Although most cost some money, there are databases available that have area code and exchange data to reference. Hope this helped!