TUTORIAL BulkVS SMS Reception/Transmission Gateway - php code

@lewy2 : If your ISP is providing you a private IP through NAT and your Asus router is also NATting your LAN/WiFi traffic then your PBX is getting its outbound traffic double natted.

So, that in itself might explain why your registrations are dropping periodically and probably frequently. With your Asus router and ISP router you probably won't be able to fix the timing issues causing the problem, so your only solution is to eliminate double natting.

By requesting a public IP from your ISP you made the right move, not only for the sms part of the dilemma but also for your dropping calls.

That said, some ISPs which assign private IP addresses to their customers by default and offer to upgrade to public IP on request also do that in very unorthodox ways. So, you might still see some issues.

Your ultimate solution might be to host your pbx on a the cloud, like on a VPS. Then the same VPS could act as your SMS gateway both ways.

If you get your public IP upgraded home network to work properly for your PBX and SMS needs then consider switching your main router to something more robust like a self-built opnsense, or pfsense, or even ipfire router.

All three are vastly superior and more secure than ANY consumer grade router. You could then use the WiFi portion of your existing router as a wireless access point (in bridge mode). Similarly once you have your public IP with your ISP, ensure that your ISP supplied router is in bridge mode and no longer performs NAT locally.

For the silly story; there are still out there ISPs which provide a private IP through the WAN uplink (they use fancy designations as carrier or infrastructure natting) which is further NATted locally by the ISP provided router (resulting in double natting) which creates triple natting to wifi or LAN clients with the addition of a consumer grade wireless router like your Asus.
 
@lewy2 : If your ISP is providing you a private IP through NAT and your Asus router is also NATting your LAN/WiFi traffic then your PBX is getting its outbound traffic double natted.

So, that in itself might explain why your registrations are dropping periodically and probably frequently. With your Asus router and ISP router you probably won't be able to fix the timing issues causing the problem, so your only solution is to eliminate double natting.

By requesting a public IP from your ISP you made the right move, not only for the sms part of the dilemma but also for your dropping calls.

That said, some ISPs which assign private IP addresses to their customers by default and offer to upgrade to public IP on request also do that in very unorthodox ways. So, you might still see some issues.

Your ultimate solution might be to host your pbx on a the cloud, like on a VPS. Then the same VPS could act as your SMS gateway both ways.

If you get your public IP upgraded home network to work properly for your PBX and SMS needs then consider switching your main router to something more robust like a self-built opnsense, or pfsense, or even ipfire router.

All three are vastly superior and more secure than ANY consumer grade router. You could then use the WiFi portion of your existing router as a wireless access point (in bridge mode). Similarly once you have your public IP with your ISP, ensure that your ISP supplied router is in bridge mode and no longer performs NAT locally.

For the silly story; there are still out there ISPs which provide a private IP through the WAN uplink (they use fancy designations as carrier or infrastructure natting) which is further NATted locally by the ISP provided router (resulting in double natting) which creates triple natting to wifi or LAN clients with the addition of a consumer grade wireless router like your Asus.
Thanks for all that info I will keep it in mind. I will point out 2 things. I have an accounts with Callcentric and Localphone that works fine, (although recently with Localphone outgoing calls started to drop every 15 minutes and I need to investigate). I also have no issues with connecting to BulkVS with MicroSip on Windows, but I had problems with bot of My Obi ATA's as well as with Incredible PBX.
VOIP is complicated!
 
My additional php script posted here handles it much like the GET variance. Download the file, unzip it and glance through, it's painless to understand the code.
Yes. I already found it and it is working OK. Who knows why the default Bulkvs SMS goes out as a GET but any new ones are POST. Kind of annoying and their documentation does not cover this. I could not figure it out until I submitted a help ticket and they sent me some additional info and a few suggestions which your newest script covered. Thanks.
 
Halea, thanks for the script. Never would be able to handle SMS without you.

I have almost no idea what I'm doing, but I got the receive portion working. I can't get the tx.php to work. I don't even know where to start looking for problems.

I don't think this is the problem but what is the "<DIRECT_MESSAGE_AUTHORIZATION_CODE_HERE>" I used the "Basic Auth Header" I found in the API Credentials section of Bulkvs.

Thanks for your patience.

-Steve
 
... what is the "<DIRECT_MESSAGE_AUTHORIZATION_CODE_HERE>" I used the "Basic Auth Header" I found in the API Credentials section of Bulkvs. ...
Hi Steve! @rLmQLqoq9bsOfGicFW

DIRECT_MESSAGE_AUTHORIZATION_CODE_HERE is a simple secret string that you create in the php file and your email message header should contain against the KEY variable to accept the message for transmission.

That's a basic safety mechanism to ensure that unauthorized people can't use your sms gateway to send out messages.

Without it anyone who knows your outbound sms gateway could send out messages since all that needs to be known is the email drop box address, the source phone number and the destination phone number.

The KEY variable used in the email subject line acts like a basic verification code to accept or reject the email message by the php transmission script. If it matches DIRECT_MESSAGE_AUTHORIZATION_CODE_HERE then the message is accepted for transmission, otherwise it is quietly rejected.

If the outbound message is created as a response to an incoming message (reply to email) the KEY variable is not used.
 
Hi Steve! @rLmQLqoq9bsOfGicFW

DIRECT_MESSAGE_AUTHORIZATION_CODE_HERE is a simple secret string that you create in the php file and your email message header should contain against the KEY variable to accept the message for transmission.

That's a basic safety mechanism to ensure that unauthorized people can't use your sms gateway to send out messages.

Without it anyone who knows your outbound sms gateway could send out messages since all that needs to be known is the email drop box address, the source phone number and the destination phone number.

The KEY variable used in the email subject line acts like a basic verification code to accept or reject the email message by the php transmission script. If it matches DIRECT_MESSAGE_AUTHORIZATION_CODE_HERE then the message is accepted for transmission, otherwise it is quietly rejected.

If the outbound message is created as a response to an incoming message (reply to email) the KEY variable is not used.

I'm getting much closer to getting things working. But not quite.

For anyone else going crazy, imap libraries may not be installed in your PHP. Easy enough to fix:

apt-get install libphp-phpmailer

Go to https://github.com/PHPMailer/PHPMailer/tree/master/src
Download these files:
Exception.php,
PHPMailer.php,
POP3.php,
SMTP.php
master.zip

I unzipped the file in :/usr/share/php/libphp-phpmailer/temp but the temp directory seems wrong.

Go to the sms-rx.php script and change the path for these files in the lines starting with "Require"



 
The sms-tx.php script says it's deleting the emails in the dedicated account. But if I run the script again it shows the same emails present.

Thunderbird shows the INBOX empty and the files deleted.. Webmail shows the files still present.

I read that deleting a file just marks it for deletion. Running imap_expunge() solved the problem

-Steve
 
Last edited:
... I read that deleting a file just marks it for deletion. Running imap_expunge() solved the problem ...
It seems to depend on the imap server (how the trash folder management is implemented) and also the imap php library version.
But, it's certainly a good reinforcement to have the function imap_expunge($inbox); called after imap_delete($inbox, $email_number); in all 5 occurrences.
 
Hi trying to set this up so far I can send a text form a mobile phone to my BulkVS number I receive an email but no message in the body of the email and the subject does not show the from and to just AAAAA and BBBBB
RXP_VER=12&SMS_GWIP=199.255.157.195&SMS_FROM=AAAAAAAAAAA&SMS_TO=BBBBBBBBBBB

I have the scripts installed on the Incredible PBX I do not have the tx set up yet still working on Part 1
I must have something set up incorrectly. Let me know if any one can point me in the correct direction to get it corrected. Thanks
 
OK got it. I updated the sms-rx script to the json one and it is working. Now off to Part 2
 
The old, sad lament, "Everything was working and stopped."

I can only send SMS messages from my Bulkvs number to another other of my BulkVS numbers.

Tried changing the BulkvsPersonalAPISecret and BulkvsPersonalAPIKey without luck.

Like Hometch asks above, shoujld REST or SOAP be used? And where do they get entered?

Thanks for the help.

Steve
 
Sorry guys for the delay in posting this note. Unfortunately I'm still recovering from surgery and I have little access to the Internet.
You have to use the SOAP API's "API KEY" and "API SECRET" values found on your personal BulkVS account portal under API > API Credentials > API Information > SOAP API.
Both the key as well as the secret strings need to be put into the script's header.
Hope this helps you get going.
 
Thanks for replying I did not know about your surgery wish you quick recovery.
I am thinking that this may be a futile effort because of A2P 10DLC effecting SMS/MMS. I don't know if an individual has to register a campaign... or what a home voip user needs to satisfy 10DLC
 
I think this info is right.

Some of my outgoing SMS go through and others fail.

Bulkvs explained that I needed a "campaign" for each DID number to send SMS. Without it, receiving phone companies may or may not accept the SMS. Bulkvs offers to set up a campaign for $10 and $6/month. Good for 40 DID numbers. Alternative companies start at $200 for the setup.

Bulkvs wanted a sample of my message. "Honey, what's for dinner?"

I get the feeling that SOAP and REST is really for mass messaging and not for my single messages.

Bulkvs says SOAP is being deprecated and it's easier to use REST.
 
Bulkvs wanted a sample of my message. "Honey, what's for dinner?"

Just another reason why when your wife say's "Sorry never got the message - thought you were taking me out to dinner" you won't know if she's telling the truth or not. LOL
 
...
I get the feeling that SOAP and REST is really for mass messaging and not for my single messages.
...
SOAP & REST are the software interfacing techniques/protocols used by BulkVS. They have no real bearing on the volume and/or frequency or still use-case of the sms messages.

When I spoke with them several months ago as I was testing my scripts, I was told that BulkVS would batch individual users' sms traffic into one umbrella campaign under their own authority which wouldn't necessitate any setup fees or recurring monthly campaign charges. Under that scenario users would only pay the sms fees shown on their standard per use fee-schedule.

My conversation with them was thorough and comprehensive and I am certain about my understanding.
Something might have changed with their upstream provider(s) most recently if they are requiring campaign registration and monthly maintenance for "non-business" users (the expression is from them based on my notes from early 2021).

I can understand that mobile phone companies are trying to qualify traffic onto their respective networks to avoid spam and flooding, but I think the underlying idea is that they are trying to turn sms/mms into a cash cow for their businesses.

But all sms traffic is not necessarily intended to cell phones either. There is a significant voip-sms user crowd out there which is also being affected by those whimsical moves.

What I am concerned about is that, this move on the part of the voip companies to submit to every caprice of the wireless carriers is going to put arbitrary boundaries around what voip can do and stifle its evolution. One perfect example of what I am trying to describe here is the so called "fines" that is being alluded to by various proividers nowadays. Some of them are talking about US $10,000 "fines" for "terms and use conditions" violations.
 
I have modified the script to support MMS

Code:
<?php
/*
This is sms-rx-json.php
A tiny script from Halea Kalas for the purpose of receiving SMS messages from BulkVS and emailing them out.

BulkVS' new incoming SMS JSON payload, received with a POST URL, has the following components:
{ 
 "From"    : "(FROM NUMBER)",  
 "To"      : [ "(TO NUMBER)" ],  
 "Message" : "(UPTO-160-CHARACTER-MESSAGE)"  
 }

MMS are sent with a URL to the media:
{ 
  "From": "(FROM NUMBER)",     
  "To": [        "(TO NUMBER)"    ],     
     "Message": "",     
      "MediaURLs": [         
       "https://s3.aws.com/file1.smil",   
       "https://s3.aws.com/file2.jpg",    
       "https://s3.aws.com/file3.txt"    
    ] 
}  
*/

# ----------------------------
# sms-rx-json-mms.php Version: 2.1
# ----------------------------

# === PRIVATE DATA SECTION =============================================

$from_email_address  = "YOUR SERVERS EMAIL ADRESS";
$reply_email_address = "YOUR REPLY EMAIL ADDRESS";
$email_to            = "DESTINATION ADDRESS";

# === END OF PRIVATE DATA SECTION ======================================

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once('phpmailer/src/PHPMailer.php');
require_once('phpmailer/src/SMTP.php');
require_once('phpmailer/src/Exception.php');

// Let's take the raw data from the request, aka POST URL:
$json = file_get_contents('php://input');

// Let's convert it into a PHP object:
$data = json_decode($json);

// Let's feed the JSON data into our old variables:
$from_phone_number = $data->From;
$to_phone_number   = $data->To[0];

# Let's determine the IP address of the gateway which rallied this SMS to our server:
#  whether ip is from share internet
if (!empty($_SERVER['HTTP_CLIENT_IP']))   
  {
    $ip_address = $_SERVER['HTTP_CLIENT_IP'];
  }
#  whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
  {
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
#  whether ip is from remote address
else
  {
    $ip_address = $_SERVER['REMOTE_ADDR'];
  }
// echo $ip_address;

$email_subject = "RXP_VER=21"."&SMS_GWIP=".$ip_address."&SMS_FROM=".$from_phone_number."&SMS_TO=".$to_phone_number;
$email_text = urldecode($data->Message);

# Let's use PHP Mailer to easily add attachments! 

$email = new PHPMailer();
$email->CharSet = "UTF-8";
$email->addReplyTo($reply_email_address, 'SMS Server');
$email->SetFrom($from_email_address, $from_phone_number);
$email->Subject =  $email_subject;
$email->AddAddress($email_to);

if (array_key_exists('MediaURLs', $data)) {
    foreach ($data->MediaURLs as $attachment) {
        $ch = curl_init($attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $file = curl_exec($ch);
        $fileinfo = pathinfo($attachment);
        $filetype = $fileinfo['extension'];
        $filename = $fileinfo['basename'];
        switch (strtolower($filetype)) {
            case "txt":
                $email_text .= "\n".$file;
                break;
            case "jpg":
                $email->addStringAttachment($file,$filename,"base64","image/jpeg");
                break;
            case "3gp":
                $email->addStringAttachment($file,$filename,"base64","video/3gpp");
                break;
            default:
                $email->addStringAttachment($file,$filename,"base64","application/octet-stream");            
        }
    }
}

$email->Body = $email_text;
$email->Send();

# The following feedback is per BulkVS SMS gateway expectations and should not be removed,
# otherwise there is a risk that the same incoming sms would be transmitted again.
echo "OK";

?>
 
Last edited:

Members online

Forum statistics

Threads
26,724
Messages
174,637
Members
20,286
Latest member
lluis.riera
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Back
Top