How to send sms from Perl

The most simple way to send SMS from Perl is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request.

how to send sms from perl
Figure 1 - How to send SMS from Perl

Perl code to send sms to mobile

The Perl sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateawy using the Perl Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

SendSms.pl
use Ozeki::Libs::Rest::Configuration;
use Ozeki::Libs::Rest::MessageApi;
use Ozeki::Libs::Rest::Message;

my $configuration = new Ozeki::Libs::Rest::Configuration();
$configuration->{ Username } = "http_user";
$configuration->{ Password } = "qwe123";
$configuration->{ ApiUrl } = "http://127.0.0.1:9509/api";

my $msg = new Ozeki::Libs::Rest::Message();
$msg->{ ToAddress } = "+36201111111";
$msg->{ Text } = "Hello world!";

my $api = new Ozeki::Libs::Rest::MessageApi($configuration);

my $result = $api->Send($msg);

print($result->stringify);
	

How to use the Perl sms example:

This Perl sms example can be used in any Perl application. To use it, you must add the Ozeki.Libs.Rest library to your project. After the library is added, you must put the using Ozeki.Libs.Rest; directive into the header section of your Perl source code. This will allow you to use the classes provided by the Ozeki.Libs.Rest library. You can use the Message class to create the SMS. You can use the MessageApi class to send the SMS to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.

Download SendSms.pl

The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendSms.pl.zip (76.0Kb)

What is in the SendSms.pl.zip file?

The SendSms.pl.zip file contains the Ozeki.Libs.Rest library, which gives you all the tools neccessary to send and receive SMS messages. You will also find the SendSms.pl file in the zip, which contains the example code to show you how to send an SMS. This example code is listed below.

Figure 2 - What is inside SendSms.pl.zip

Dependencies

To use the Ozeki::Libs::Rest library you have to install a few modules:

Ubuntu

$ sudo apt-get install libdatetime-perl
$ cpan
$ install JSON
$ install UUID::Generator::PurePerl
$ install UUID::Object
	

Code 1 - How to install the dependencies on Linux

How to install Perl dependencies in Linux (Video tutorial)

In this video, we show you how to install the dependencies of Perl on Linux OS. The video will start with opening a terminal and will end with the dependencies installed. The video is only 1:10 long but it explains the process in a detailed way. If you watch the whole video, you will have no problem recreating the steps.

Windows

cpanm DateTime
cpanm UUID::Generator::PurePerl
cpanm UUID::Object
cpanm JSON
	

Code 2 - How to install the dependencies on Windows

How to install Perl dependencies on Windows (Video tutorial)

In this video, we show you how to install the dependencies of Perl on Windows OS. The video will start with opening a command prompt and will end with all the dependencies installed. The video is only 46 seconds long but it explains the process in a detailed way. The process is easy to do so with the help of this video, you will have no problem following the steps.

How to send SMS from Perl (Simple guidelines)

To send SMS from Perl:

  1. Install a HTTP API user
  2. Enable Log communication events on the Advanced tab
  3. Download then extract the SendSms.pl.zip file
  4. Open the sendsms.pl file in any text editor
  5. Launch Ozeki SMS Gateway app
  6. Run SendSms.pl Perl code in the command prompt
  7. Check the logs to see if the SMS sent

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send SMS from Perl, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your Perl code in Windows Notepad or Visual studio Code. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.

After the environment is setup, you can run your Perl code.

HTTP API url to use send sms from Perl

To send SMS from Perl, your Perl will will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the Perl sms application is running, this can be 127.0.0.1. If it is installed on a different computer it should be the IP address of that computer.

http://127.0.0.1:9509/api?action=rest
	

HTTP authentication to use send sms from Perl

To authenticate the Perl sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In Perl you can use the following code to do this encoding:

my ($self, $Username, $Password) = @_;
my $Username_Password = "${Username}:${Password}";
my $Username_Password_encoded = encode_base64($Username_Password);
return "Basic ${Username_Password_encoded}";
	

For example if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==. To send

HTTP request header to send SMS from Perl

To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.

Content-Type: application/json
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
	

HTTP request to send SMS from Perl

To submit the SMS, your Perl application will send an HTTP request similar to the one below. Note, that this requst contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient number and the messages text.

POST /api?action=sendmsg HTTP/1.1
Connection: TE, close
Content-Length: 341
Content-Type: application/json; charset=utf8
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 127.0.0.1:9509
Te: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.52

{
	"messages": [
		{
			"message_id": "fa32293f-db00-11eb-804d-ffacbeab4160",
			"to_address": "+36201111111",
			"text": "Hello world!",
			"create_date": "2021-07-02T06:44:40",
			"valid_until": "2021-07-09T06:44:40",
			"time_to_send": "2021-07-02T06:44:40",
			"submit_report_requested": true,
			"delivery_report_requested": true,
			"view_report_requested": true,
			"tags": []
		}
	]
}
	

HTTP response received by the Perl sms example

Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the messages submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.123 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Tue, 29 Jun 2021 11:48:38 GMT
Server: 10/10.3.123
Transfer-Encoding: chunked

{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "Messages queued for delivery.",
	"data": {
	  "total_count": 1,
	  "success_count": 1,
	  "failed_count": 0,
	  "messages": [
	    {
	      "message_id": "fa32293f-db00-11eb-804d-ffacbeab4160",
	      "from_station": "%",
	      "to_address": "+36201111111",
	      "to_station": "%",
	      "text": "Hello world!",
	      "create_date": "2021-07-02 06:44:40",
	      "valid_until": "2021-07-09 06:44:40",
	      "time_to_send": "2021-07-02 06:44:40",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": false,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ],
	      "status": "SUCCESS"
	    }
	  ]
	}
}
	

Connect your SMS gateway to the mobile network and create an HTTP API user account

We assume, you have already installed Ozeki SMS Gateway, and you have connected it to the mobile network. In order to be able to send SMS to a mobile phone from Perl, you need to setup an HTTP API user account in Ozeki SMS Gateway. In this video, we are going to explain to you the process of creating a new HTTP API user account. The video will start with opening the Ozeki SMS gateway application and will take you to have the created HTTP API user. In the process, you will learn how to configure a new user and how to turn on the logging option. The video is only 30 seconds long and it explains the process in a very detailed way. You will have no problem understanding the steps.

Video 1 - How to setup an HTTP API user account

How to send SMS from Perl using the Perl sms api (Video tutorial)

This video shows you how to download the SendSms.pl.zip file from this page, and how to open the SendSms.pl file in Notepad. If watch the video, you will notice, that the contents of the SendSms.pl zip are placed into the Windows desktop. You will also see, that we open the SendSms.pl file with windows Notepad.

Video 2 - How to download and set up the solution above

Perl sms example: SendSms.pl

On Figure 1, you can see the source code of the example project. If you are familiar with the Perl programming language, feel free to modify the code to your liking. This code will send an SMS to the phone number stored in the toAddress variable, with the text stored in the Text variable.

Figure 3 - SendSms.pl file

How to check that the SMS has been accepted by the HTTP user

After the SMS has been submitted, it is a good idea to check your SMS gateway, to see what it has received. You can check the log by opening the HTTP user's details from the Ozeki SMS Gateway managment console. The following video shows you what to look for.

Video 3 - Sending SMS with the Perl code above

How to check that the SMS has been sent to the mobile network

The final step in verifying the procedure is to take a look at the logs of the mobile network connection. You might have to turn on logging in the configuration of the connection before you send the message to see the logs. If logging is enabled you will see the phone number and the text of the message you have sent.

Video 4 - How to test if the request was accepted by the SMPP client

How receiving an SMS on an Android phone looks like (Video tutorial)

In this video, you will see how receiving an SMS message on an Android device looks like. It will start with an ordinary android home page. You will see the notification popping up and then the opened Messages app with the SMS visible in it. The video is only 18 seconds long but it features the whole process.

Video 5 - SMS message received on the mobile phone

Conclusion

This guide was meant to show how to send SMS in Perl with the help of Ozeki SMS Gateway. If everything was read carefully, messaging with your customers in Perl with Ozeki SMS Gateway should not cause any kind of problem. It has to be highlighted that Ozeki SMS Gateway allows you to log SMS messages into a database and offers various reporting capabilities to give more information about the SMS connection.

Continue reading on Ozeki's tutorial page where more information can be found about subjects like multiple SMS sending and SMS scheduling in Perl.

The only thing to do now is to download Ozeki SMS Gateway and start working!

More information