TUTORIAL BulkVS SMS Reception/Transmission Gateway - php code

Halea

Well-Known Member
Joined
Aug 12, 2016
Messages
1,132
Reaction score
874
I have a fully functional code, but I can't post it here as the generates a server error whether I enter is as text or code. I tried to attach it as a text file which again resulted in some errors.
What's the proper way of posting long documents here? (A few hundreds of lines)


Edit: Following Ward's advice it has been attached as a tar.gz file

Once you have both the receiving and transmitting scripts in place, any incoming sms sent to you by Bulkvs is going to trigger sms-rx.php, which will extract its content and send it to your user account as an email with a judiciously formatted subject line showing up the incoming phone number etc.
Then, you can simply reply to that email with your outgoing message (without changing anything to the subject line), which will go sit in a drop box monitored by sms-tx.php.
Next, cron activated (every minute) sms-tx.php will fetch your reply email, analyze the subject line which is a Re: to the incoming message subject line (with the proper phone numbers), and if both phone numbers as well as a usable text body are available will put it in an sms format and send it out to the BulkVS gateway. - That simple.

If you want to send out an sms message without replying to a received message, simply write an email to the drop box while ensuring that the subject line contains something like SMS_FROM=12223334444&SMS_TO=12223334444
--------------------------------------------------------------------------

Edit 5/24/2020: I found out that there is a bug or rather a logic error in the way the SMS "from" and "to" addresses are handled. I fixed it :2guns:. If you're interested in getting the updated version please drop a word. The one posted here (v 1.0) is perfectly usable and I have been using it for several weeks without realizing the issue. So, what is it about?

Well, I designed sms-rx.php and and sms-tx.php in such a way that the user has minimal overhead to enter during messaging.
sms-rx.php parses bulkvs' "get url" post and creates a subject line showing sms-from, sms-to and the ip address from where it got the message. The body of the email is the message portion of the post.

When the intended user receives the email from sms-rx it looks like an ordinary email message to which it can simply answer by clicking on the "reply" button of its email tool; at which time the subject line is automatically prefaced by Re: or something equivalent (determined by the email tool). The original email message also has a "Reply-To:" field initialized with the intended email drop box for the sms-tx process. Therefore, as the user clicks on reply, a preformatted email reply message is made with the same subject line and the proper return email address so that it ends up in the drop box periodically checked by sms-tx.php

Now, obviously the subject line is not adjusted for the fact that the return message still shows the original sms sender and receiver phone numbers. Since this is a reply message the phone numbers need to be swapped, and the sms-tx script does just that flawlessly (and mindlessly). There is nothing wrong up to this point.

What happens if one is not replying to an incoming message but simply creating an outgoing basic message? Well, he creates the subject line himself and assumes that SMS_FROM is the sender's phone, and SMS_TO is the receiver's phone!

Well, sms-tx.php v 1.0 script still reverses those two phone numbers without properly checking that this is not a reply message but a direct message :mad2:

How should it check though? Well, the IP address used in incoming messages subject line is used as an indication that we are dealing with a return message, therefore the phone numbers need to be swapped. If there is no gateway ip field in the subject line then there is no need for swapping them. :arabia:

The reason for which I didn't catch the problem until now is that I was using an email template to quickly create test messages, and once I finished testing I kept using the same template for actual sms sending, and my template had the two labels swapped. :death:

Earlier today my system processed its 1,000th sms message! :auto:

Update 1/16/2021: I've replaced the attachment file with the latest scripts. The sms reception script remains largely the same but a script version number was added to the email message subject file format. It's quite unobtrusive.
As for the sms transmission script, it fixes the phone number swap issue, but also adds an authorization code to the direct-sent message subject line, without which anyone who can send mail to the sms gateway mailbox could use the gateway.
The attached file contains both the reception and transmission scripts. You need to extract the php blocks as mentioned in the comments and create 2 separate php files, one for reception, one for transmission. I call them sms-rx.php and sms-tx.php but you can call them as you wish.
sms-rx.php needs to be put in the root of your web server. If you want to run it on your PBX it will be under /var/www.html. For my home use I do run it on my PBX too, the impact on performance is absolutely negligible. It's invoked every time when there is a new sms message that BulkVS wants to deliver to your system.
sms-tx.php could be run from any location as long as the proper php material is available (see the comments in the attachment file). I also run it on my home PBX. sms-tx.php needs to be triggered every 10 to 60 seconds so that the email messages intended to be sent out as sms are processed in a timely fashion. cron is my favorite way of triggering it, but other ways are possible, like with a watch command, or in a bash loop. This script checks an email box dedicated to sms messages. It scans through once per execution. If it finds a message (or more) with the proper format it processes it. If malformed or unrelated messages are found (like spam) they are simply deleted. The goal of this approach is to keep that email box as light as possible so that every time it's scanned the script doesn't have to go through tons of non-sms material which would slow it down significantly. So, under normal use conditions the mail box is close to empty.

Update 8/30/2021 : SMS reception has been enhanced with SMS forwarding to another phone number in a new script called sms-rx-jef.php. It can be found here.
 

Attachments

Last edited:
This is great. Thank you very much for posting this. I am interested in creating a PHP front for allowing customer service agents to communicate with customers - this helps for creating a proof of concept to start the creation of such a tool. Does anyone know if there is anything prepackaged out there to allow us to do two-way SMS (ticket style) using a gateway of our choosing, such as BulkVS?
 
This is great. Thank you very much for posting this. I am interested in creating a PHP front for allowing customer service agents to communicate with customers - this helps for creating a proof of concept to start the creation of such a tool. Does anyone know if there is anything prepackaged out there to allow us to do two-way SMS (ticket style) using a gateway of our choosing, such as BulkVS?
I'm glad you found it useful. I have more than a dozen customers at this point using BulkVS to whom I either provided sms sending/receiving building blocks or full-fledged custom apps or back-end processing systems. It's quite straight forward but each project has its own emphasis, so it's difficult to turn it into a general purpose "gateway", at least for now.
For ticket management, like many developers do, I use zoho's interactive system to get going quickly. On a couple of occasions where the project required complete privacy I had to build the interface from ground up. With all the open source material available that's not difficult but it still rediscovering the wheel and effort duplication.
To answer your question, there are sms gateway software out there that can interface bulkvs, but most (if not all) such turn key gateways transit your messages through their own servers and charge you per-message or on subscription basis. Those solutions are fine for many projects but not for those who are privacy focused like the user base I deal with.
 
Last edited:
Thank you for sharing your code! It was fun getting RX.php adapted to use PHPmailer so it would work with my hosting provider and for some reason I had difficulty using my providers cronjob interface to run the TX code, so I just used cron-job.org and that worked flawlessly. So far I'm about 15 messages and counting in the last 12 hours. Thanks again @Halea!
 
Last edited:
Anyone on here willing to provide a novice the files I need to upload to the webserver to get this BulkVS SMS going or an install script please?

Sorry, @Halea , I downloaded your tar.gz file and then have no idea what to do from there. :(

Thank you in advance.
 
Thank you for sharing your code! It was fun getting RX.php adapted to use PHPmailer so it would work with my hosting provider and for some reason I had difficulty using my providers cronjob interface to run the TX code, so I just used cron-job.org and that worked flawlessly. So far I'm about 15 messages and counting in the last 12 hours. Thanks again Halea!
@smurfturf : I'm glad it worked for you too. I'll be helping out @Prits later when I have access to my PBX, at which time I will be posting the final version of the sms transmission script. It's slightly different from the one that I originally posted as it checks to see if the message being transmitted is a response to an incoming sms or a fresh new one. The reply to an incoming sms swaps the phone numbers, but otherwise there is no phone number swapping. That's the bug or logic error I described.

There are several ways to trigger the transmission script. The cron job is one way, and probably the best. But on my test system I have XFCE4 installed on my PBX. So, on that machine I start the transmitter in a terminal window using:
watch -n 10 "curl https://MyPBX.MyDomainName.com/sms-tx.php >/dev/null 2>&1"
which invokes the transmission script every 10 seconds. I can let it run forever or simply zap the terminal and stop the transmission part.
 
Anyone on here willing to provide a novice the files I need to upload to the webserver to get this BulkVS SMS going or an install script please?

Sorry, @Halea , I downloaded your tar.gz file and then have no idea what to do from there. :(

Thank you in advance.
@Prits : I'll get back to you a bit later and guide you through the process. The RX part is very easy to setup. The script posted is up to date. For transmission, I'll post the final version. Unfortunately I'm not near my machine right now.
 
@Prits : I'll get back to you a bit later and guide you through the process. The RX part is very easy to setup. The script posted is up to date. For transmission, I'll post the final version. Unfortunately I'm not near my machine right now.

no probs/rush at all, I appreciate it all!
 
no probs/rush at all, I appreciate it all!
I posted the latest version of the scripts. Please take it down and unpack/decompress it. You'll find a text file that you should open a text editor (not a word processor though). Read through and you'll see that it is made of 2 parts. The first is for the reception script. The second for the transmission script.
You need to extract the first php block into a new text file that you call for instance sms-rx.php. The second php block goes into a second text file named sms-tx.php. A php block is what's between <?php and ?> (many lines later). It's really straight forward. Just copy and paste into a blank file.
Once you have your 2 files, edit them so that they contain your own personal data (like email addresses, BulkVS code, etc.) Each script has a section clearly marked as "personal/private data" where you need to enter your own.
Then you need to put the sms-rx.php file into the root directory of the PBX's web server. It will be /var/www/html, but might be different depending on your PBX software version or config. Finally to get it triggered, you need to go to BulkVS' settings panel and put-in your URL, which will look like: http://YourPBX.com/sms-rx.php
Note that you need to use your actual server's FQDN instead of YourPBX.com.
Try to get it going this far and let me know. With this much you will be able to receive sms messages sent to your BulkVS DID on your incoming email.
Then I'll guide you through the sms transmission script.
 
@Halea , thanks for the updated code. I will look through it and integrate it into my deployment. I love the idea of a code for outbound sms messages!
 
I posted the latest version of the scripts. Please take it down and unpack/decompress it. You'll find a text file that you should open a text editor (not a word processor though). Read through and you'll see that it is made of 2 parts. The first is for the reception script. The second for the transmission script.
You need to extract the first php block into a new text file that you call for instance sms-rx.php. The second php block goes into a second text file named sms-tx.php. A php block is what's between <?php and ?> (many lines later). It's really straight forward. Just copy and paste into a blank file.
Once you have your 2 files, edit them so that they contain your own personal data (like email addresses, BulkVS code, etc.) Each script has a section clearly marked as "personal/private data" where you need to enter your own.
Then you need to put the sms-rx.php file into the root directory of the PBX's web server. It will be /var/www/html, but might be different depending on your PBX software version or config. Finally to get it triggered, you need to go to BulkVS' settings panel and put-in your URL, which will look like: http://YourPBX.com/sms-rx.php
Note that you need to use your actual server's FQDN instead of YourPBX.com.
Try to get it going this far and let me know. With this much you will be able to receive sms messages sent to your BulkVS DID on your incoming email.
Then I'll guide you through the sms transmission script.


Hello @Halea ,

Thank you for the help...

so I did what you told me... my files are called sms-rx.php and sms-tx.php

they are uploaded,

I added the URL to my BulkVS portal,

I tried several testing of an SMS enabled DID and I do NOT get/receive any email messages in my inbox.

BUT, if I call the file myself in a web browser by going to the URL, I get a response 'OK' and I do get an email!

for example, if I put the following in my web browser:

http://mypbx.com/sms-rx.php?from=0987456321&to=1234567890&message=Hello, How are you

Thats as far as I have got! lol

Thank you
@Prits
 
Last edited:
@Prits Did you also, chown sms-rx and sms-tx to asterisk:asterisk and chmod on them to 777? If using GMail, did you create a "less secure" login and password so GMail would accept the message? Try sending yourself a message while you tail -f /var/log/mail.log and look for "Message accepted" or messages with "Rejected."
 
@Prits : Do you have sendmail installed on that machine? As you can see towards the bottom of the sms-rx.php script the command "mail()" is used to transmit the email message out:
# Following requires that sendmail is installed and operational on the web server where this php file is running.
mail($email_to,$email_subject,$email_text,$email_headers);

If not installed, it can be installed with "sudo apt install sendmail". Then you need to configure it so that it can send out emails properly. For instance, if you are on a restricted network you need to use a smarthost upstream, etc.

Also, prior to that point when mail() is called the sms message is parsed, and immediately above the mail() instruction the three bits of parsed information are made available in 3 variables called $email_headers, $email_subject, and $email_text.

Print those out with an "echo" command and see if they are properly initialized/filled in. If they are not the issue might be related to the php engine that your system is running. Although I'd be surprised. I think your issue is with the mail command.
 
Last edited:
@Prits Did you also, chown sms-rx and sms-tx to asterisk:asterisk and chmod on them to 777? If using GMail, did you create a "less secure" login and password so GMail would accept the message? Try sending yourself a message while you tail -f /var/log/mail.log and look for "Message accepted" or messages with "Rejected."

Hey @kenn10 , I do have the files chowned to asterisk:asterisk and 777 permissions

I just want to clarify, that I DO RECIEVE emails if I manually execute the script by calling the php file in a web browser ha

Also, I do have the SMS enabled and my URL set at BulkVS

NOTE: I have even added these IPs to the iptables as stated at BulkVS:

Inbound messages will come from one of the following IPs: 199.255.157.195 or 69.12.88.195.
 
Last edited:
@Prits : Here is my edited file that I am using. There is some code in there to help with connection issues. Just remove the // before //$mail->SMTPDebug = 2; That will post everything that is going on while trying to send your email. Keep in mind that you'll need to disable it by adding the // later so it just posts an OK for Bulk VS. One of the upsides to part of the code that I added is that if it doesn't send the email, it won't post an OK, it will post "message could not be sent", or it will post the actual error message. Keep in mind that my file really isn't cleaned up to share, I've just been setting it up and I've left all of @Halea 's code in case I wanted to go back to it. I don't understand code as well as most on this forum, but I thought I'd share what I have done to get it to work. You'll need to install phpmailer. I used this as a reference:


There is also a link for a dedicated server in the linked page as well.

I have a not for profit that gets free hosting from Dreamhost and a free Google Apps account from Google, so I decided to use those services to test this out.

Again, I apologize for the messy code additions, but hopefully this will show you another way to get the email portion working so you can hopefully move forward in your installation.
 

Attachments

@Prits : Do you have sendmail installed on that machine? As you can see towards the bottom of the sms-rx.php script the command "mail()" is used to transmit the email message out:
# Following requires that sendmail is installed and operational on the web server where this php file is running.
mail($email_to,$email_subject,$email_text,$email_headers);

If not installed, it can be installed with "sudo apt install sendmail". Then you need to configure it so that it can send out emails properly. For instance, if you are on a restricted network you need to use a smarthost upstream, etc.

Also, prior to that point when mail() is called the sms message is parsed, and immediately above the mail() instruction the three bits of parsed information are made available in 3 variables called $email_headers, $email_subject, and $email_text.

Print those out with an "echo" command and see if they are properly initialized/filled in. If they are not the issue might be related to the php engine that your system is running. Although I'd be surprised. I think your issue is with the mail command.

Hello @Halea,

So I do use sendmail and my pbx does send emails out etc.

I just want to clarify, that I DO RECIEVE emails if I manually execute the script by calling the php file in a web browser ha

ie:

http://mypbx.com/sms-rx.php?from=0987456321&to=1234567890&message=Hello

I get the email, I sent it to you in a private conversation here with all the headers etc

Also, I do have the SMS enabled and my URL set at BulkVS

NOTE: I have even added these IPs to the iptables as stated at BulkVS:

Inbound messages will come from one of the following IPs: 199.255.157.195 or 69.12.88.195.

hmmmmm
 
Last edited:
@Prits I apologize for my post earlier. I had started that before you posted that you are receiving email messages. If you are receiving email messages, my guess is that there is something potentially wrong with the url in bulkvs. Here is the url I have posted with them:
Good Luck! I hope you get it working soon.
 

Members online

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