Tuesday 14 August 2018

Send Text Messages with PHP

Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get the volume of texts that I hear my younger female cousins send.  Thousands and thousands of them each month.  WTF are all of these texts for?  Here's a thought:


omg did you hear?

no wut

omg i can't believe you didn't hear

lol tell me!

jenny and mark were holding hands

omfg does john no?

ok i made it up, ur so lame

Riveting.  Jokes aside, text messaging can be an extremely useful way to get out of calling that person you hate calling communicate quickly and efficiently.  Many websites are now offering text message notifications instead of email notifications, which can be nice in the case of time-sensitive information.  After a bit of research, I found out how easy it was so send text messages using PHP, so that I can integrate text messaging into my apps!  Let me show you how!

The Methodology
Unbeknownst to me, sending text messages can be as easy as sendmail, because you can send your text to an email address and it will be delivered.  There are two pieces of information you must know:  the phone number and the carrier's text message email domain.  Needing to know the recipient's carrier is not ideal, but necessary.  Luckily Kevin Jensen has compiled a list of carriers and domains:


Phone companies have internal lookups for phone carriers but developers like you and I don't get access to them, so knowing the carrier is  a must.  To send a text message, you email {phoneNumber}@{carrierDomain}.

The PHP
PHP's provided method for sending emails is the mail function.  Its usage is quite simple:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Using the mail function and the text message email format, sending a text message is as easy as:

// Call Jenny
mail("5558675309@txt.att.net", "", "Your packaged has arrived!", "From: David Walsh <david@davidwalsh.name>\r\n");
Note that no subject line is provided, and more importantly, a FROM header is within the last parameter so that the recipient knows where the text message has been sent from.

Who knew it was that easy?  I always assumed text messaging had its own protocol and all that magic.  Apparently not!  Spend a few minutes playing around with text messaging yourself;  it's one of those things that's both neat to do and useful!

0 comments:

Post a Comment