Send SMS Text Messages Using HTML & PHP!

October 24th, 2009

sms

Every once and a while I like to send my friends prank text messages to make them think someone is watching them. The problem is every time I use a free service, they some how cram an ad or spam into the message. I’m going to show you how you can send your own text messages using HTML/PHP. Demo & Download Included!

First

Create the form that you’ll put in your HTML:

<form id="sms" name="sms" method="post" action="sms/send_sms.php">

<input name="to" type="text" id="to" />

<select name="carrier" id="carrier">

           <option value="att">AT&amp;T</option>
           <option value="verizon">Verizon</option>
           <option value="tmobile">T-Mobile</option>
           <option value="sprint">Sprint</option>
           <option value="virgin">Virgin Mobile</option>

</select>

<textarea name="message" rows="6" id="message"></textarea>

<input type="submit" name="Submit" value="Send" />

</form>

Second

Now we need the PHP to send the text message:

<?php
$to = $_POST[‘to’];
$carrier = $_POST[‘carrier’];
$message = stripslashes($_POST[‘message’]);
$headers = ‘From: QuickText <no-reply@quicktext.com>’ . "\r\n";

if ((empty($to)) || (empty($message))) {
header ("Location: ../sms_error.php");
}

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

header ("Location: ../sms_success.php");
}

else if ($carrier == "tmobile") {
$formatted_number = $to."@tomomail.net";
mail("$formatted_number", " TXT Message", "$message" , "$headers");

header ("Location: ../sms_success.php");
}

else if ($carrier == "sprint") {
$formatted_number = $to."@messaging.sprintpcs.com";
mail("$formatted_number", " TXT Message", "$message" , "$headers");

header ("Location: ../sms_success.php");
}

else if ($carrier == "att") {
$formatted_number = $to."@txt.att.net";
mail("$formatted_number", " TXT Message", "$message" , "$headers");
header ("Location: ../sms_success.php");
}

else if ($carrier == "virgin") {
$formatted_number = $to."@vmobl.com";
mail("$formatted_number", " TXT Message", "$message" , "$headers");

header ("Location: ../sms_success.php");
}

?>

The code above creates the MINIMUM basics that you need to send a text message. So I went ahead and added some simple styling, validation, support for the iPhone browser and more. Check out the demo and download the files below!

Feel free to use this code, build upon it and share it. But if you would be so kind, leave the footer link in the files. Thank you!

*Javascript used in this code example was written by my wife Lauren Schaible.

demodownload

Tags: , , , ,

4 Comments

  1. Dennis Monsewicz Says:

    Josh,

    Thanks for posting this… I have been working on an SMS solution for a client of mine and have been diving into some code to perfect mine and this is perfect.

    -Dennis

  2. Joshua Schaible Says:

    Dennis,

    I’m really glad this helped you out. I’d love to see what you end up with. Keep rockin.

    -Joshua

  3. Dan Silver Says:

    Thank you so much!!!!
    I will be implementing this text message script into my program I made. (if you want to see it you can click on my name)
    Thanks!

  4. Volomike Says:

    I have used this system previously for my AT&T mobile. The only drawback is that the service providers sometimes add extra text into the text, shortening its capacity. These providers aren’t very thoughtful in that regard.

    Another system is to enroll with Clickatel, but you have to follow their guidelines and get approved, and then there’s a small fee.

    Note also on Linux you can find a command (it’s a Perl script, actually) called sendEmail. I have a handy Bash script loaded up through /etc/crontab that uses curl command line to check my Gmail via the ATOM RSS feed thing, parses it, and then uses the sendEmail command to SMS text me the headlines of new mail.

Leave a Reply