How to send multiple sms from Perl
The simplest way to send multiple 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.
Perl code to send multiple sms to mobile
The Perl sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateway 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.
SendMultipleSms.pluse 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 $msg1 = new Ozeki::Libs::Rest::Message(); $msg1->{ ToAddress } = "+36201111111"; $msg1->{ Text } = "Hello world 1"; my $msg2 = new Ozeki::Libs::Rest::Message(); $msg2->{ ToAddress } = "+36202222222"; $msg2->{ Text } = "Hello world 2"; my $msg3 = new Ozeki::Libs::Rest::Message(); $msg3->{ ToAddress } = "+36203333333"; $msg3->{ Text } = "Hello world 3"; my $api = new Ozeki::Libs::Rest::MessageApi($configuration); my $result = $api->Send(( $msg1, $msg2, $msg3 )); 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 messages 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 SendMultipleSms.pl
The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendMultipleSms.pl.zip (76.1Kb)
What is in the SendMultipleSms.pl file?
The SendMultipleSms.pl file contains the Ozeki.Libs.Rest library, which gives you all the tools necessary to send and receive SMS messages. You will also find the SendMultipleSms file in the zip, which contains the example code to show you how to send an SMS. This example code is listed below.
How to send multiple sms from Perl (Quick steps)
To send multiple sms from Perl:
- Setup Strawberry Perl
- Download the SendMultipleSms.pl.zip file
- Extract the .zip file from Downloads folder
- Open the SendSms.pl file in Notepad or Visual Studio Code
- Launch Ozeki SMS Gateway
- Create a HTTP API user in Ozeki
- Run SendMultipleSms.pl Perl code in the command prompt
- Check the Sent box in Ozeki SMS Gateway
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 Visual studio. 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 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 request contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient's number and the message's text.
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 message's submission.
HTTP/1.1 200 OK User-Agent: OZEKI 10.3.123 (www.myozeki.com) Content-Type: application/json; charset=utf8 Last-Modified: Fri, 02 Jul 2021 16:00:35 GMT Server: 10/10.3.123 Transfer-Encoding: chunked { "http_code": 200, "response_code": "SUCCESS", "response_msg": "Messages queued for delivery.", "data": { "total_count": 3, "success_count": 3, "failed_count": 0, "messages": [ { "message_id": "ab4c1e78-db41-11eb-8298-c00c4d4e2ea7", "from_station": "%", "to_address": "+36201111111", "to_station": "%", "text": "Hello world 1", "create_date": "2021-07-02 14:27:45", "valid_until": "2021-07-09 14:27:45", "time_to_send": "2021-07-02 14:27:45", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" }, { "message_id": "ab4c4523-db41-11eb-838d-c00c4d4e2ea7", "from_station": "%", "to_address": "+36202222222", "to_station": "%", "text": "Hello world 2", "create_date": "2021-07-02 14:27:45", "valid_until": "2021-07-09 14:27:45", "time_to_send": "2021-07-02 14:27:45", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" }, { "message_id": "ab4c59cd-db41-11eb-82c0-c00c4d4e2ea7", "from_station": "%", "to_address": "+36203333333", "to_station": "%", "text": "Hello world 3", "create_date": "2021-07-02 14:27:45", "valid_until": "2021-07-09 14:27:45", "time_to_send": "2021-07-02 14:27:45", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" } ] }
Perl sms example: SendMultipleSms.pl
In this video, you are going to see the process of downloading and setting up the SendMultipleSMS.pl project. It will start with downloading the project and will end with the setup done. You will learn all the steps of the setup process you need to do to have a working solution. The video is 1:44 long but it has all the steps shown in a very detailed way. After watching it, you could download and set up the solution on your own.
The example code
On Figure 3, you can see the example code. This example code will send multiple SMS messages to multiple recipients. If you are familiar with the Perl programming language, feel free to modify the code to your liking. It will send SMS to all the phone numbers stored as 'ToAddress' variable, with the text stored in the Text variables.
How to use the example project file (Video tutorial)
In this video, you will see the example project in work. We will show you what you need to do to make the project send multiple SMS messages with only one run. The video is only 1:12 long and contains all the information you need to launch the program. It will start with the opened project and will end with all the messages sent.
Summary
The guide above explained the steps of multiple SMS sending in Perl with Ozeki SMS Gateway. If every step was followed carefully, you should be able to reach out to multiple clients with one Perl code and Ozeki SMS Gateway. With this solution, a large number of customers can get their SMS at the same time. Important to note that Ozeki SMS Gateway offers high performance, meaning it can send up to 1000 SMS per second.
Make sure that you don't finish reading here, visit Ozeki's tutorial page where more information can be found about similar topics, like SMS receiving and SMS scheduling in Perl.
The only thing to do now is to download Ozeki SMS Gateway and let the work begin!
More information
- Perl send SMS with the HTTP rest API (code sample)
- Perl send multiple SMS with the HTTP rest API (code sample)
- Perl schedule SMS with the HTTP rest API (code sample)
- Perl receive SMS with the HTTP rest API (code sample)
- Perl delete SMS with the HTTP rest API (code sample)
- How to download the latest Perl SMS library from Github