In a recent project I was asked to come up with a solution to send text messages out as a premium feature on a client’s membership site. This is not something I had ever done before, but I figured I would compile all the information I gathered into one simple tutorial for those looking to do the same
The Free Option – E-mail
If you didn’t already know, text messages can actually be sent through e-mail to most cell phone carriers. For example, I am an AT&T customer. To send myself a text message without a phone, I could open up gmail, and send an e-mail to: [email protected] I would get the text message just like I would any other message.
Applying this to PHP is easy enough in terms of actual code. The mail function will do the trick quite nicely. If you have your number handy and know the carrier the number is using, you can look up the cell phone carrier here. To send a message to myself, it was as easy as:
$number = "999-999-9999";
$carrier = "@txt.att.net";
$message = "This is a text";
$sent = mail("$number@$carrier",'Test',$message);
echo $sent ? "Mail sent" : "Mail NOT sent";
//mail(e-mail address, subject, body);
//the subject isn't required and can be left blank if desired
This can create dilemnas however. It requires that you know both the number AND the cell phone carrier. With number portability, the cell phone carrier can change, and unless a user notifies you of this information, you would have no way of knowing their carrier changed. This option just wasn’t feasible for us as we needed it to work flawlessly no matter if they changed carriers or whatever the case may have been. If we had the number, that’s all the information that we should need. To ask the user to maintain more information than that up to date was asking too much.
I could see this being a useful way to send text messages if I was just looking to build some sort of notification service for myself. i.e. if one of my daily cron scripts was failing, something along those lines. This is not a scalable reliable solution for any sort of solution bigger than just a few users.
The Paid Option – SMS Gateways
There are numerous SMS gateways out there, with different pricing points and APIs. SMS gateways aren’t free, but they provide a more secure and trusted way of getting the job done. I remembered doing some research on this very subject about a year ago and the name Twilio stuck. They are who I ended using and a quick and easy tutorial is below. They cost 1 cent per text message, not the cheapest, but very reasonable for what we were trying to accomplish.
How to send a text message with Twilio
You’ll first need an account with them, so go sign up. You’ll be provided with a user id and authentication token that you can plug into the appropriate variables below. Next, download the official Twilio PHP library from here.
//include the official Twilio PHP library
$accountid = "#######";
$token = "#######";
//declare a new instance of the twilio services class
$client = new Services_Twilio($AccountSid, $AuthToken);
$mynumber = "##########";
$to = "##########";
$body = "this is a text message";
$client->account->sms_messages->create($from, $to, $body);
//voila! message sent
For any amount of scale at all, I’d recommend using a gateway like Twilio. You don’t have to worry about keeping track of the carrier and in my experience they have been extremely reliable. Any feedback or suggestions, let me know in the comments below.