Script to send SMS via Goip4 gateway for ABillS

I will give an example of a PHP script to which I send SMS messages through the GoIP4 gateway.

First, we choose tariffs with only monthly charges (I chose everything, excluding with daily charges, since there are fewer of them):

SELECT name, tp_id FROM tarif_plans;
SELECT name, tp_id FROM tarif_plans WHERE tp_id NOT IN(1,2,4,8,9,10,39,63,64,65,106,107,109,110,114,117,118,119)

Actually, this is the script itself, I run it from cron on a separate server (where I installed the GoIP4 SMS server):



$db=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_base);
mysqli_query($db, "SET NAMES 'utf8'");

$Result = mysqli_query($db,
SELECT im.uid, uc.value, b.deposit, im.activate, tp.month_fee
FROM internet_main im
LEFT JOIN users u ON (im.uid = u.uid)
LEFT JOIN users_pi u_pi ON (im.uid = u_pi.uid)
LEFT JOIN bills b ON (u.bill_id=
LEFT JOIN tarif_plans tp ON (tp.tp_id = im.tp_id)
LEFT JOIN users_contacts uc ON (im.uid = uc.uid)
WHERE im.tp_id NOT IN(1,2,4,8,9,10,39,63,64,65,106,107,109,110,114,117,118,119)
AND uc.type_id=1
AND DATEDIFF(CURDATE(), im.activate) = 27
AND< tp.month_fee
AND u.reduction=0
AND u.company_id=0
AND u_pi._actual_mob=1;

while($Row = mysqli_fetch_array($Result))
$uid = $Row['uid'];
$phone =$Row['value'];
$deposit = intval($Row['deposit']);
$activate = $Row['activate'];
$date_end = date("d-m", strtotime("$activate +30 days"));
$fee = intval($Row['month_fee']);


$mess_ = "Інтернет відключиться $date_end. На рах. $deposit грн, тариф $fee. КОМПАНІЯ";
$mess =  rawurlencode($mess_);

print("$phone           $mess_\n");

$r = file_get_contents("$phone&method=2&Memo=$mess");
mysqli_query($db, "INSERT INTO sms VALUES(NULL, '$uid', '$phone', NOW(), '$mess_', '$activate')");

There is no “_actual_mob” table in the standard abills database, I have created an additional field with a checkbox that can be used to indicate the relevance of a mobile phone in a user account, 1 is relevant, 0 is not by default.
You can also add another SQL query that removes old records, for example older than 1 year.

For the logs sent by SMS, I created the following table in the ABillS database:

  `id` mediumint(254) NOT NULL AUTO_INCREMENT,
  `uid` mediumint(254) NOT NULL,
  `phone` varchar(32) NOT NULL,
  `datetime` datetime NOT NULL,
  `text` varchar(254) NOT NULL,
  `activate` datetime NOT NULL,
  PRIMARY KEY (`id`)

In the future, you can view the contents of the table through the rwizard module or the Settings – SQL Commander menu:


I use Zabbix for monitoring, so I’ve also opened the configuration of Zabbix agent:


И добавил параметр:

UserParameter=sms_sent_today,mysql -u root -e "SELECT count(*) FROM abills.sms WHERE datetime LIKE '`date +%Y-%m-%d`%';" -s -N

And added a data item with an interval of 12 hours, a trigger warning of the excess of the sent SMS, and a schedule on the Zabbix server to see on the chart how many SMS are sent daily.

See also my articles:
SMS sending script via Goip4 gateway
Installing and configuring GoIP SMS server
SQL queries for ABillS

Подписаться на IT Blog (RU) по Email
Subscribe to IT Blog (EN) by Email

Did my article help you? How about buying me a cup of coffee as an encouragement? Buy me a coffe.

Leave a Reply