Tuesday 26 March 2013

Use Google Voice to send SMS with PHP


Use Google Voice to send SMS with PHP

Although many prefer to send and receive text messages on their mobile device (the traditional way), Google Voice can capture SMS sent to your phone number and display it on the web, much like an email inbox:
Screenshot of Google Voice web interface
You can then forward SMS to your Gmail account, bypassing the need to monitor your separate Google Voice inbox.
If this is not the future of SMS, I’m not sure what is.
But it gets better. Not only can you receive SMS in your email account, you can compose and reply to SMS directly through email, meaning you’ll never have to thumb out text messages on a tiny mobile keypad ever again! (If you don’t want to, that is. I’m thinking of full keyboards when I say this – obviously you can still email on a mobile device.)
Is it possible this can get even better?? Sure, why not!?
Email is, of course, operational using scripting languages, such as PHP. I think you know where I’m going.
Using Google’s SMTP servers, you can send SMS using PHP, meaning any application you write can fire off text messages!

The fine print

Hype aside, let’s get down to business with the details and limitations.
First off, this only works when someone has already sent you a text message to your Google Voice number, with “Text Forwarding” turned on:
Screenshot of Google Voice setting
In this case, you’ll receive the incoming text message to your Gmail inbox, with a “From” address that is unique to the person who sent it:
Screenshot of Gmail message
This is that person’s SMS-email address. Anytime you compose a new email to that address (from your Gmail account), it will result in a text message being sent to them.
The problem is you have no way of making this universal to any phone number, since the SMS-email address has some unique characters in it, which can’t be copied.
So the person must literally send you a text message, so you can obtain their unique SMS-email address. If anyone knows a way around this, please let me know.

The code

Regardless, we can still use PHP to send SMS – we just need to know the unique SMS-email address ahead of time.
This is a very simple script, and only requires the PHPMailer class files.
include "phpmailer/class.phpmailer.php";

$from_email = 'matthom@gmail.com';
$from_name = 'Matt Thommes';
$password = 'YOUR_GMAIL_PASSWORD_HERE';
$to_email = '18472304701.xxxxxxxxxx.-VCNPKgbLM@txt.voice.google.com';
$to_name = 'Michelle Thommes';

$text_message = 'Hi dear. This text is being sent from PHP. Sweet, eh?';

$mail = new PHPMailer();
$mail -> IsSMTP(true);
$mail -> Host = 'ssl://smtp.gmail.com:465';
$mail -> SMTPAuth = true;
$mail -> Username = $from_email;
$mail -> Password = $password;
$mail -> SetFrom($from_email, $from_name);
$mail -> AddAddress($to_email, $to_name);
$mail -> MsgHTML($text_message);

if ( !$mail -> Send() )
{
  echo "Mailer Error: " . $mail -> ErrorInfo;
}
else
{
  echo "Message sent!";
}
As you can see, very minimal scripting required.

No comments:

Post a Comment