our systems use a table to queue the messages and then this script to process the messages.
how/where you put the data into the table is up to you. this table is a shared messaging table for our sms and email messages, so you an rename or remove fields fields as you require. d_sms_queue_processor.php is what we named it when creating it via phpmaker… create whatever filename you like
TABLE:
CREATE TABLE `sms_queue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`processed` tinyint(4) DEFAULT '0',
`sender` varchar(255) DEFAULT NULL,
`recipient` varchar(255) DEFAULT NULL,
`cc` varchar(255) DEFAULT NULL,
`bcc` varchar(255) DEFAULT NULL,
`subject` varchar(255) DEFAULT NULL,
`content` text,
`UserId` int(11) DEFAULT NULL,
`DateCreated` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CreatedBy` int(11) DEFAULT NULL,
`LastModified` datetime DEFAULT NULL,
`ModifiedBy` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
PREMISE:
pretty simple, every x time you run the script from the cron, it checks the table for records where processed = 0. if processed =0 then it just loops grabs the data and calls SendSMSMessage(). Once sent it updates the records processed to 0 or 1 depending if it was successfully sent.CODE:
create a custom file and add the following:
useee \Twilio\Rest\Client as twiSMS;
if (file_exists('../vendor/twilio/sdk/Twilio/autoload.php'))
require '../vendor/twilio/sdk/Twilio/autoload.php';
$sqlSelect = "SELECT * FROM sms_queue q WHERE q.processed=0 ORDER BY DateCreated DESC";
$result = Execute($sqlSelect);
while(!$result->EOF()) {
$msgHeader = array("id" => $result->fields['id'], "sender" => $result->fields['sender'], "recipient" => $result->fields['recipient'], "cc" => $result->fields['cc'],
"bcc" => $result->fields['bcc'], "subject" => $result->fields['subject'], "content" => $result->fields['content'], "smtp_result" => "", "message_id" => $result->fields['id'], "datetime" => "");
SendSMSMessage($msgHeader);
sleep ( 3 );
$result->MoveNext();
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Send an SMS using Twilio's REST API
//
function SendSMSMessage($msgHeader, $other params) {
$sid = "TWILIO_SMS_SID"; // Your Account SID from www.twilio.com/console
$token = "TWILIO_SMS_TOKEN"; // Your Auth Token from www.twilio.com/console
$Mobile_No = $msgHeader['recipient'];
$msg = $msgHeader['content'];
if($Mobile_No == '' || !isValidPhoneNumber) {
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', CurrentUserIP(), $Mobile_No, 'Invalid Mobile Number', '', 'SMS Send');
return;
}
$msg = RemoveHtml($msg);
try {
$client = new twiSMS($sid, $token);
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', 'client->create', $client, $sid, $token, 'Twilio SMS Initialized');
}
catch(Exception $e) {
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', 'client->create', $e, $sid, $token, 'Twilio SMS Initialization Failed');
exit();
}
try {
$message = $client->messages->create(
$Mobile_No, // Text this number
array(
'from' => "YOUR TWILIO_SMS_NUMBER), // From a valid Twilio number
'body' => $msg
)
);
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', CurrentUserIP(), $Mobile_No, $msg, $message, 'SMS Send Completed');
$bSMSSent = 1;
}
catch(Exception $e) {
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', 'messages->create', $e, $Mobile_No, $msg, 'Failed');
$bSMSSent = 0;
}
// Set the processed flag to true if the message was successfully sent.
$sqlUpdate = "UPDATE sms_queue q SET q.processed=" .$bSMSSent. " WHERE q.id=".$msgHeader['id'];
$rs = Execute($sqlUpdate);
}
IN YOUR CRON, add a line similar to this:
*/5 * * * * cd /srv/www/cgi-bin/ && ./mycron_jobs.shin mycron_jobs.sh: (don’t forget to: chmod +x mycron_jobs.sh), add the following lines
note: make sure you “CD” into the location of the script, otherwise this will not work.
cd /LOCATION_OF_YOUR_CODE/ (EX: /srv/www/htdocs/myapp/daemons
php -f d_sms_queue_processor.php &
note 1:in the class file, rem out or remove the following if running via a cron job
/*
if (!$Security->canReport()) {
$Security->saveLastUrl();
$this->setFailureMessage(DeniedMessage()); // Set no permission
$this->terminate(GetUrl("index.php"));
return;
}
*/