FOOD FOR THOUGHT Basic SMS forwarding script for BulkVS DIDs

Halea

Well-Known Member
Joined
Aug 12, 2016
Messages
1,132
Reaction score
874
I've just finished testing the php script shown below. It works quite well. It basically takes an incoming sms message and sends it out to a forwarding number.
There are some limitations that you should be aware of:
1) It only works on BulkVS DIDs which are SMS enabled.
2) The forwarded message shows the original target phone number as BulkVS doesn't allow spoofing the original source sms phone number during the re-transmission.
This is a significant annoyance if the forwarded SMS needs to be replied to with the intention of reaching the original sender, but not so much of a problem if it's mostly used for two factor authentication codes etc. which do not require a reply.
<?php
/*
This is sms-rx-json-fwding.php, a tiny script from Halea, for the purpose of forwarding SMS messages
received on a BulkVS DID to another phone number through BulkVS' sms transmission service.
This script makes use of BulkVS' new JSON type incoming SMS payload (received with a POST URL),
which has the following components:
{
"From" : "(FROM NUMBER)",
"To" : [ "(TO NUMBER)" ],
"Message" : "(UPTO-160-CHARACTER-MESSAGE)"
}
Note that this script will only work if curl and php5-curl are both installed and enabled with the following:
sudo apt-get install curl
sudo apt-get install php5-curl
sudo php5enmod curl
sudo service apache2 restart
*/
# === PRIVATE DATA SECTION ==========================================
# BulkVS SMS Transmission Service Personal Parameters:
$BulkvsSmsTransmitterUrl = "https://portal.bulkvs.com/sendSMS";
$BulkvsPersonalApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$BulkvsPersonalApiSecret = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
# Forwarding phone number definition:
$fwd_to_phone = "<YOUR_11_DIGIT_PHONE_NUMBER_FOR_FORWARDING_HERE>";
# === END OF PRIVATE DATA SECTION ====================================
// 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 variables:
$ori_fr_phone = $data->From;
$ori_to_phone = $data->To[0];
# curl command execution via curl php - works well:
$ch = curl_init( $BulkvsSmsTransmitterUrl );
# Let's determine payload for the outgoing sms:
$sms_text = "(Fr:".$ori_fr_phone."/To:".$ori_to_phone."/Fw:".$fwd_to_phone.") ".urldecode($data->Message);
$payload = json_encode( array( "apikey" => $BulkvsPersonalApiKey,
"apisecret" => $BulkvsPersonalApiSecret,
"from" => $ori_to_phone,
"to" => $fwd_to_phone,
"message" => $sms_text,
) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
# 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";
?>
To make this work on your PBX you simply need to put the above script in a text file under /var/www/html that you could name sms-rx-json-fwding.php assuming that you already have curl and php5-curl installed and enabled. Obviously you would need to enter the proper values for XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY and <YOUR_11_DIGIT_PHONE_NUMBER_FOR_FORWARDING_HERE>.
Then you need to configure your BulkVS DID which needs to be forwarded to the 11 digit phone number entered in the script. For that you would login into your BulkVS panel and go to Messaging > Message Webhooks. There, you can create a new hook in which you will use a URL like http://<yourPBXserver.com>/sms-rx-json-fwding.php assuming that you called your script file as suggested earlier. (You should also adjust your server name, adjust the TCP port number if it's different from 80, use https instead of http if you're using SSL on your server, etc.)
Oh! also, at that stage make sure that Delivery Receipt for that particular webhook is set to False.
Finally you would go to Inbound > DIDs - Manage and pick the DID that you want to configure, and set its Messaging Webhook value to the entry that you've just created in the previous step.
The php script gets triggered every time a message is sent via the URL defined in the webhook. So there is no need to run a periodic process starter like cron etc.

Please note that the forwarded message will include the original source phone number (Fr), the original target phone number (To) and the forwarding phone number (Fw) so that there is no doubt about the whereabouts of the message.

Regarding the phone number spoofing issue I'll contact BulkVS tomorrow morning and see if there is anything that can be done. Meanwhile if anyone here has any suggestions please let me know.

Update 8/30/2021 : There is an enhanced version of this script which can do both sms emailing and forwarding to another phone number simultaneously. The new script called sms-rx-jef.php can be found here.
 
Last edited:
I communicated with BulkVS today and it sounded like, for the time being, there is no solution other than including the originating phone number in the body of the forwarded text as I already implemented.

Also, if you decide to use my script above, remember to turn off Delivery Receipt as the current version of the script forwards the delivery receipt message too, therefore causing an endless loop. I'll modify it so that it scans the content of the message and if it appears to be a delivery confirmation then that message is not forwarded.
Unfortunately I am not sure what to scan for exactly, as the info they provided me is different from what I actually saw during my tests.
 
I communicated with BulkVS today and it sounded like, for the time being, there is no solution other than including the originating phone number in the body of the forwarded text as I already implemented.
I don't think this is going to change, so I wouldn't be holding my breath over it. You have never been able to source a text from a number that wasn't assigned to you. Considering that 2021 has been the year where things have been changing in regards to Spoofing/SPAMing/Robo actions for both voice and messaging, I highly doubt they are going to take something like this and change it.

The one thing to consider is not all voice numbers have the expectation of being able to *send messages*. Landline numbers are a prime example of this. Sure they can receive (if capable) which gives you a TTS result (text read back to you over a call) but they can't send. Since messaging has always been a bit stricter than voice that means the chances of the other side accepting that message could be very slim since it's a number that shouldn't be sending messages. Honestly, the only numbers that are ever guaranteed 100% messaging (SMS/MMS) are mobile numbers since that is where all this pretty much originated from. People have complained for years about how VoIP numbers can't receive or send messages from/to certain places (like shortcodes) or can do text but no media, etc. etc.

The other thing that needs to be taken into consideration is that there is now two classifications of number types for messaging with the introduction of 10DLC. There is now Person-to-Person and Application-to-Person. Basically, your mobile number that is tied to your SIM card/mobile device is considered P2P. Sure you can go group texts but you are limited to how many recipients you can send to. You really can't auto send stuff to large numbers of people, etc. etc.

On the flip side, VoIP numbers automatically fall into the Application-to-Person category (which 10DLC is) and those numbers now have to comply with new things like The Campaign Registry. Come October 2021 major players such as ATT, T-Mobile/Sprint, US Celluar and about a half dozen more will start either greylisting or flat out blocking any messaging sent to or over their networks (such as to their MNVO's) from numbers classified as A2P that do not have a submission in TCR. At the same time ATT and T-Mobile/Sprint have also modified their costs for A2P to send to or receive from their networks. That has been in effect for a few months now. All my messaging records actually now tell me who the carrier was and the listed campaign (from those numbers that supply it, so ATT/T-Mobile for sure) as well as tell me who the receiving carrier was in case of outgoing.

Just keep in mind that getting listed in The Campaign Registry is *not carrier related*. It is up to the owners of the numbers that will be used for **sending messages** to get their business and their campaign types (and yes casual conversing is a campaign type) registered. It's actually a PITA because I wanted to submit on behalf of my customers, which is allowed, but I needed all sorts of extra information like the business's EIN, etc.

So I don't think a measure that is in place now to validate ownership/identifying the sender of messaging is going to be suddenly changed considering how much has to be done now with all the rules coming into place.
 
So I don't think a measure that is in place now to validate ownership/identifying the sender of messaging is going to be suddenly changed considering how much has to be done now with all the rules coming into place.
That's not what I originally commented that I want. I want BulkVS to take an incoming SMS/MMS to one of their numbers and forward it to a number I specify. No option for spamming or fraud there. The message is simply passed to another number. Anveo Direct has had that feature for a long time. I believe Vitelity has it too, though its been awhile since I moved my DID's from them and may not remember correctly.
 
@Halea, thanks for sharing the SMS scripts - quite useful.

I'm successfully using the SMS forward to email one. However the SMS forward to phone number stops after this stage:

pcap.png
The PBX is fully functional and runs on Debian 10 based on the official install document.

My observation is that the statements:
Code:
sudo apt-get install php5-curl
sudo php5enmod curl

do not work on Debian 10. However, these ones do:
Code:
sudo apt-get install php5.6-curl
sudo phpenmod curl
.
...and as I said the SMS forward to email works just fine (based on the same requirements.)

Any suggestion where to start troubleshooting why the forward to number part doesn't work?
 
Last edited:
@sortons : Yes indeed on Debian 10 and Ubuntu 20.04 the package used is php5.6-curl and to enable the curl php module you have to run phpenmod curl. The script borrowed code from my earlier scripts which was based on earlier php versions on older Debian and Ubuntu OS. But actually any version of php curl support would work as we are truly using plain vanilla base functionality here. Same for module enablement command. I actually test on an Ubuntu 20.04 server and I was able to use php5, 7 and 8 for the same.
 
8/30/2021: New Script
I've just finished testing a new script called sms-rx-jef.php, which combines BulkVS SMS to email functionality with SMS forwarding to another phone number capability featured in the earlier script. Emailing or sms forwarding can be selectively turned off so that the script does only sms emailing or sms to sms forwarding. It can also do both at the same time and neither if you've turned both features off (then it wouldn't serve any purpose, would it ?!) :rolleyes:
Here comes the script:
<?php
/*
This is "sms-rx-jef.php", a tiny script from Halea, for the purpose
of receiving SMS messages from BulkVS and emailing them out while
also forwarding them to another phone number.

Emailing is optional and to be enabled requires a valid email address
assigned to the variable "$email_to" below.
To keep it off set it to "".

SMS forwarding is optional and to be enabled requires a valid phone
number for forwarding assigned to the variable "$fwd_to_phone".
To keep it disabled set it to "".

See proto values in "Private Data Section" below.
*/

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

# eMail forwarding parameters:
$from_email_address = "[email protected]";
$reply_email_address = "<REPLY-TO_ADDRESS_POINTING_AT_OUTGOING_SMS_MAILBOX>";
$email_to = "";
$email_to = "<USER_EMAIL_ADDRESS_WHERE_INCOMING_SMS_IS_DELIVERED>";

# BulkVS SMS Transmission Service Personal Parameters:
$BulkvsSmsTransmitterUrl = "https://portal.bulkvs.com/sendSMS";
$BulkvsPersonalApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$BulkvsPersonalApiSecret = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";

# Forwarding phone number definition:
$fwd_to_phone = "";
$fwd_to_phone = "<YOUR_11_DIGIT_PHONE_NUMBER_FOR_FORWARDING_HERE>";

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

$json = file_get_contents('php://input');
$data = json_decode($json);
$from_phone_number = $data->From;
$to_phone_number = $data->To[0];
$email_text = urldecode($data->Message);

if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip_address = $_SERVER['REMOTE_ADDR'];
}

$email_headers = "From: ".$from_email_address."\r\n"."Reply-To: ".$reply_email_address.", ".$email_to."\r\n";
$email_subject = "RXP_VER=22"."&SMS_GWIP=".$ip_address."&SMS_FROM=".$from_phone_number."&SMS_TO=".$to_phone_number;

if ($email_to != "" )
{
# Let's send the sms message to the designated email recipient if there is one defined:
mail($email_to,$email_subject,$email_text,$email_headers);
}

if ($fwd_to_phone != "")
{
#Let's run the sms forwarding material below if there is a phone number to forward to:
$ch = curl_init( $BulkvsSmsTransmitterUrl );
$sms_text = "(Fr:".$from_phone_number."/To:".$to_phone_number."/Fw:".$fwd_to_phone.") ".$email_text;
$payload = json_encode(array("apikey"=>$BulkvsPersonalApiKey,"apisecret"=>$BulkvsPersonalApiSecret,"from"=>$to_phone_number,"to"=>$fwd_to_phone,"message"=>$sms_text));
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
}

echo "OK";
?>
The email forwarding functionality remains compatible with the "email to sms" script posted here some time ago.
 
Last edited:
I've just finished testing the php script shown below. It works quite well. It basically takes an incoming sms message and sends it out to a forwarding number.
There are some limitations that you should be aware of:
1) It only works on BulkVS DIDs which are SMS enabled.
2) The forwarded message shows the original target phone number as BulkVS doesn't allow spoofing the original source sms phone number during the re-transmission.
This is a significant annoyance if the forwarded SMS needs to be replied to with the intention of reaching the original sender, but not so much of a problem if it's mostly used for two factor authentication codes etc. which do not require a reply.

To make this work on your PBX you simply need to put the above script in a text file under /var/www/html that you could name sms-rx-json-fwding.php assuming that you already have curl and php5-curl installed and enabled. Obviously you would need to enter the proper values for XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY and <YOUR_11_DIGIT_PHONE_NUMBER_FOR_FORWARDING_HERE>.
Then you need to configure your BulkVS DID which needs to be forwarded to the 11 digit phone number entered in the script. For that you would login into your BulkVS panel and go to Messaging > Message Webhooks. There, you can create a new hook in which you will use a URL like http://<yourPBXserver.com>/sms-rx-json-fwding.php assuming that you called your script file as suggested earlier. (You should also adjust your server name, adjust the TCP port number if it's different from 80, use https instead of http if you're using SSL on your server, etc.)
Oh! also, at that stage make sure that Delivery Receipt for that particular webhook is set to False.
Finally you would go to Inbound > DIDs - Manage and pick the DID that you want to configure, and set its Messaging Webhook value to the entry that you've just created in the previous step.
The php script gets triggered every time a message is sent via the URL defined in the webhook. So there is no need to run a periodic process starter like cron etc.

Please note that the forwarded message will include the original source phone number (Fr), the original target phone number (To) and the forwarding phone number (Fw) so that there is no doubt about the whereabouts of the message.

Regarding the phone number spoofing issue I'll contact BulkVS tomorrow morning and see if there is anything that can be done. Meanwhile if anyone here has any suggestions please let me know.

Update 8/30/2021 : There is an enhanced version of this script which can do both sms emailing and forwarding to another phone number simultaneously. The new script called sms-rx-jef.php can be found here.
hey i new with the setup the SMS can you help me in this seccion
 
8/30/2021: New Script
I've just finished testing a new script called sms-rx-jef.php, which combines BulkVS SMS to email functionality with SMS forwarding to another phone number capability featured in the earlier script. Emailing or sms forwarding can be selectively turned off so that the script does only sms emailing or sms to sms forwarding. It can also do both at the same time and neither if you've turned both features off (then it wouldn't serve any purpose, would it ?!) :rolleyes:
Here comes the script:

The email forwarding functionality remains compatible with the "email to sms" script posted here some time ago.
Both sides of the script work well - thanks.
 
hey i new with the setup the SMS can you help me in this seccion
Please read this current thread in its entirety as well as this one. You will find all the information that's needed to get the scripts working. If you have basic working knowledge of php scripting you will have it up and running in no time. If you run into any issues please post the specifics so that you can be provided with help and guidance.
 

Members online

No members online now.

Forum statistics

Threads
26,724
Messages
174,636
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