User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

I like sip stations ability to receive SMS straight to the PBX (FreePBX and Asterisk). However I almost never use their web interface to get and send messages. I wanted them forwarded to my gmail account so this is what I did.

First you must have some experience using shell  and have access to your asterisk server.

You will have also had to setup email on your asterisk server for this to work. I set mine up so I could get voice mails emailed to me.

What you'll do in this tutorial:

You will login to your asterisk server via SSH

Open MySQL and alter a table.

Create a PHP script

Create a cron job.

 

So lets starts:

First log in to mysql on your asterisk server using the database asterisk.

ssh to your asterisk server and login and type the following (or copy paste if using putty):

mysql

use asterisk;

alter table sms_messages add forwarded int(11) default '0';

exit;

 

now create a new file for your php script:
don't forget to swap out your email address with youremail@someaddress in the script:
new file location /var/www/html/sendsms.php
paste the following from <?php thru ?> into your nano editor. (please understand that I wrote this script on my cell phone in nano so well, this is it)

nano /var/www/html/sendsms.php

<?php 
$conn =  mysql_connect("localhost", "root", "");
if (!$conn) {  
  die("Connection failed: " . mysql_error()); 
} 
echo "Connected successfully"; 
mysql_select_db('asterisk'); 
$sql="select * from sms_messages where forwarded='0' limit 1"; 
$reader = mysql_query($sql);
echo "<table><tr><th>id</th><th>from</th><th>to</th><th>message</th><th></tr>”;
$id="";                                                                    
$to="";                                                                    
$from="";                                                                  
$body="";                                                                                             
$body="";                                                                  
$dt="";
while($row = mysql_fetch_assoc($reader)){ $mys = "<tr> <td>".$row['id']."</td> <td>".$row['from']."</td> <td>".$row['to']."</td> <td>".$row['body']."</td> <td>".$row['tx_rx_datetime']."</td>
</tr>"; echo $mys; $id=$row['id']; $to=$row['to']; $from=$row['from']; $body=$row['body']; $dt=$row['tx_rx_datetime']; } echo "</table>"; if($id != ""){ $mys = "to: ".$to . "\r\nfrom: ".$from."\r\ntime: ".$dt."\r\nmessage: ".$body; mail("youremail@someaddress","sms:".$to . " " . $from ,$mys); mysql_query("update sms_messages set forwarded='1' where id='".$id."'"); } mysql_close($conn); ?>

Add cron job using your server ip address. Swap out the 192.168.1.9 with yours:

crontab -e

* * * * * wget -O /dev/null -o /dev/null 192.168.1.9/sendsms.php

Now if you go to your asterisk server and go to the page sendsms.php you should see 1 message (assuming you have received SMS messages).
If you see a message you will get an email shortly with the SMS message.