Skip to content

Commit caec5a4

Browse files
Add iSmartSMS service for sending SMS
This commit introduces the iSmartSMS service which allows the sending of SMS via the ismartsms.net (Oman) gateway. Several files are created including services, configuration, and provider to handle the messaging functionality. A .env example has also been added to guide the user on necessary environment configurations for the SMS service.
0 parents  commit caec5a4

File tree

8 files changed

+205
-0
lines changed

8 files changed

+205
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/vendor
3+
/composer.lock

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Abdulaziz Al Rashdi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## IsmartSMS Library for Laravel
2+
This basic library help you to send SMS via ismartsms.net (Oman)
3+
4+
## Installation:
5+
```bash
6+
composer phpawcom/ismartsms
7+
```
8+
9+
In your .env, add the following:
10+
```dotenv
11+
ISMARTSMS_USER_ID="Your ismartsms API username"
12+
ISMARTSMS_PASSWORD="Your Password"
13+
```
14+
15+
16+
### publish config:
17+
This is an optional step, not really needed if you are using .env for configuration
18+
```bash
19+
php artisan vendor:publish --provider "S4D\Laravel\IsmartSMS\IsmartSMSProvider"
20+
```
21+
22+
The S4D\Laravel\IsmartSMS\IsmartSMSProvider is auto-discovered and registered by default.
23+
If you want to register it yourself, add the ServiceProvider in config/app.php:
24+
```php
25+
'providers' => [
26+
S4D\Laravel\IsmartSMS\IsmartSMSProvider::class,
27+
]
28+
```
29+
For alias:
30+
```php
31+
'aliases' => [
32+
S4D\Laravel\IsmartSMS\IsmartSMS::class,
33+
]
34+
```
35+
36+
## Usage:
37+
Example code:
38+
```php
39+
if(IsmartSMS::SendSMS('{8 digits phone number}', '{SMS Content}')){
40+
// TODO: SMS has been sent, some action here
41+
}else{
42+
// TODO: SMS couldn't be sent, some action here
43+
}
44+
```
45+
46+
If you want to see the result of SMS gateway:
47+
```php
48+
print_r(IsmartSMS::getRawResults());
49+
```
50+
You can add setFlashSMS() if you want the SMS to disappear after reading:
51+
```php
52+
IsmartSMS::setFlashSMS()->SendSMS('{8 digits phone number}', '{SMS Content}')
53+
```

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "phpawcom/ismartsms",
3+
"description": "Send SMS via iSmartSMS gateway (Oman)",
4+
"type": "library",
5+
"license": "MIT",
6+
"version": "1.0.0",
7+
"autoload": {
8+
"psr-4": {
9+
"S4D\\Laravel\\IsmartSMS\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "Abdulaziz Al Rashdi",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"minimum-stability": "dev",
19+
"extra": {
20+
"laravel": {
21+
"providers": [
22+
"S4D\\Laravel\\IsmartSMS\\IsmartSMSProvider"
23+
],
24+
"aliases": {
25+
"Thawani": "S4D\\Laravel\\IsmartSMS\\IsmartSMS"
26+
}
27+
}
28+
},
29+
"require": {
30+
"php": ">=8.1",
31+
"ext-curl": "*",
32+
"guzzlehttp/guzzle": "^7.2"
33+
},
34+
"require-dev": {
35+
"orchestra/testbench": "8.x-dev"
36+
}
37+
}

src/IsmartSMS.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace S4D\Laravel\IsmartSMS;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use S4D\Laravel\IsmartSMS\Services\IsmartSMSService;
7+
8+
/**
9+
* @method static bool SendSMS(string $receiver, string $message)
10+
* @method static array getRawResults()
11+
* @method static void setFlashSMS()
12+
* @method static void unsetFlashSMS()
13+
*/
14+
class IsmartSMS extends Facade {
15+
protected static function getFacadeAccessor() {
16+
return IsmartSMSService::class;
17+
}
18+
}

src/IsmartSMSProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace S4D\Laravel\IsmartSMS;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use S4D\Laravel\IsmartSMS\Services\IsmartSMSService;
7+
8+
class IsmartSMSProvider extends ServiceProvider {
9+
public function boot(){
10+
$this->mergeConfigFrom(__DIR__.'/config/ismartsms.php', 'ismartsms');
11+
$this->publishes([
12+
__DIR__.'/config/ismartsms.php' => config_path('ismartsms.php')
13+
]);
14+
}
15+
16+
public function register(){
17+
$this->app->bind('IsmartSMS', fn() => new IsmartSMSService());
18+
}
19+
}

src/Services/IsmartSMSService.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace S4D\Laravel\IsmartSMS\Services;
4+
5+
use Carbon\Carbon;
6+
use GuzzleHttp\Client;
7+
8+
class IsmartSMSService {
9+
public ?array $rawResults;
10+
private bool $isFlash = false;
11+
public function setFlashSMS(): static {
12+
$this->isFlash = true;
13+
return $this;
14+
}
15+
public function unsetFlashSMS(): static {
16+
$this->isFlash = false;
17+
return $this;
18+
}
19+
public function sendSMS($receiver, $message): bool {
20+
$result = false;
21+
$language = (int) preg_match("/\p{Arabic}/u", $receiver)? '64' : '0';
22+
$client = new Client();
23+
$response = $client->request('POST', config('ismartsms.service_url'), [
24+
'form_params' => [
25+
'UserID' => config('ismartsms.user_id'),
26+
'Password' => config('ismartsms.password'),
27+
'Message' => $message,
28+
'Language' => $language,
29+
// 'ScheddateTime' => Carbon::now(),
30+
'MobileNo' => strlen($receiver) == 8? '968'.$receiver : $receiver,
31+
'RecipientType' => $this->isFlash == 1? 9 : 1
32+
],
33+
]);
34+
try {
35+
$body = json_decode($response->getBody()->getContents(), true);
36+
$this->rawResults = $body;
37+
if($body['Code'] == 1){
38+
$result = true;
39+
}
40+
} catch (\Exception $e) {
41+
throw new \Exception('Unable to send SMS, error: '.$e->getMessage());
42+
}
43+
return $result;
44+
}
45+
public function getRawResults(){
46+
return $this->rawResults;
47+
}
48+
}

src/config/ismartsms.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
return [
3+
'user_id' => env('ISMARTSMS_USER_ID', ''),
4+
'password' => env('ISMARTSMS_PASSWORD', ''),
5+
'service_url' => env('ISMARTSMS_URL', 'https://ismartsms.net/RestApi/api/SMS/PostSMS'),
6+
];

0 commit comments

Comments
 (0)