In this post I will describe how to make a simple Email to SMS Gateway, using Portech MV37x and PHP code running on a Linux server.
Operation is simple: the code will poll the configured imap a folder, and if detect a new email it will send a Short Message Service (SMS) using the Portech hardware device. The recipient’s mobile number must be specified in the email subject (the subject must be a number or the email will be ignored) and the text will be the body in the email itself (truncated to 140 chars).
Att.: In this script we are using an IMAP email account and the PHPmailer library.
<!--?php </p> <p>require_once('/var/spool/mail2sms/PHPMailer-master/class.phpmailer.php');</p> <p>$hostnamemail = '{URL IMAP ACCOUNT:143}INBOX';<br ?--> $usernamemail = 'IMAP USERNAME'; $passwordmail = 'IMAP PASSWORD'; $hostportech = 'PORTECH IP'; $usernameportech = 'PORTECH USERNAME'; $passwordportech = 'PORTECH PASSWORD'; function sendSMS($_number,$_message){ global $hostportech; global $usernameportech; global $passwordportech; $debug = false; $fp = fsockopen($hostportech, 23, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno) \n"; die; } sleep(2); $cmd = $usernameportech."\r"; fputs($fp, $cmd, strlen($cmd)); if ($debug) echo fread($fp, 128); sleep(1); $cmd = $passwordportech."\r"; fputs($fp, $cmd, strlen($cmd)); if ($debug) echo fread($fp, 128); sleep(1); $cmd = "module\r"; fputs($fp, $cmd, strlen($cmd)); if ($debug) echo fread($fp, 128); sleep(2); $cmd = "ate1\r"; fputs($fp, $cmd, strlen($cmd)); if ($debug) echo fread($fp, 128); sleep(1); $cmd = "at+cmgf=1\r"; fputs($fp, $cmd, strlen($cmd)); sleep(2); $cmd = "at+cmgs="$_number"\r"; fputs($fp, $cmd, strlen($cmd)); sleep(2); $cmd = "$_message\r\x1a"; //Ctrl-Z fputs($fp, $cmd, strlen($cmd)); sleep(2); fclose($fp); } set_time_limit(3000); $numerorand= rand(5, 15); $inbox = imap_open($hostnamemail,$usernamemail,$passwordmail); $mailconsoggettonumerico=false; $emails = imap_search($inbox,'UNSEEN'); if($emails) { $overview = imap_fetch_overview($inbox,$emails[0],0); $message = imap_fetchbody($inbox,$emails[0],1); $soggetto=$overview[0]->subject; if(is_numeric($overview[0]->subject)) { $mailconsoggettonumerico=true; } if(!$mailconsoggettonumerico) { imap_close($inbox,CL_EXPUNGE); exit; } if (strlen($message)>140) { $message=substr($message, 0,140); } sendSMS($soggetto,$message); } imap_close($inbox,CL_EXPUNGE); ?>
Using cron is possible to automate the entire process: the code will poll the imap account and if a new email arrives it will be analyzed and will be trasmitted via SMS the new message !
Linkografia
PhpMailer
Downloading Gmail attachments in PHP