Sending Text Messages in PHP

Aug 07, 2010

Recently I've had to implement an alert system in PHP were the alerts are sent via a text message. The concept is very simple - use an email to SMS gateway. Essentially, you send an email to a phone companies gateway server, that server then relays the email to the phone number specified via a SMS. Since PHP has the ability to send emails, the whole process is quite easy.

1. First you need to acquire a list of SMS gateways you are going to support. For an extensive list, check out Wikipedias List of SMS Gateways. For this tutorial, I will only be using AT&T Wireless (number@txt.att.net) and Verison (number@vtext.com).

2. Create a simple HTML form.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"></meta>
	<title>SMS</title>
</head>
<body>
<form action="POST">
		<label for="provider">Provider: </label>
		<select><option value="txt.att.net">AT&T Wireless</option><option value="vtext.com">Verizon</option></select>
		<label for="number">Number: </label>
		<input type="number" />
		<label for="subject">Subject: </label>
		<input type="subject" />
		<label for="message">Message: </label>
		<input type="message" />
		<input type="submit" /></form>
</body>
</html>

3. Add the necessary PHP code to send the email

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"></meta>
	<title>SMS</title>
</head>
<body>
	<?php
		if(!empty($_POST['number'])) {
			mail($_POST['number'] . $_POST['provider'], $_POST['subject'], $_POST['message']);
        }
	?>
<form action="POST">
		<label for="provider">Provider: </label>
		<select><option value="@txt.att.net">AT&T Wireless</option><option value="@vtext.com">Verizon</option></select>
		<label for="number">Number: </label>
		<input type="number" />
		<label for="subject">Subject: </label>
		<input type="subject" />
		<label for="message">Message: </label>
		<input type="message" />
		<input type="submit" /></form>
</body>
</html>

You will want to do some better error checking and validation, but thats it!