esendex/sdk
PHP 8.3+ SDK for Esendex SMS messaging. Install via Composer and authenticate with your account to send SMS and retrieve inbox messages. Includes dispatch and inbox services, uses cURL, and supports autoloading via Composer or bundled loader.
Install the Package Add the SDK via Composer in your Laravel project:
composer require esendex/sdk:^3.0
Laravel’s autoloader will handle the rest—no manual require needed.
Configure Credentials
Store Esendex credentials in .env:
ESENDEX_ACCOUNT_REF=EX000000
ESENDEX_USERNAME=user@example.com
ESENDEX_PASSWORD=your_password
First Use Case: Send an SMS
Create a helper class (e.g., app/Services/EsendexService.php):
use Esendex\Authentication\LoginAuthentication;
use Esendex\DispatchService;
use Esendex\Model\DispatchMessage;
class EsendexService {
public function sendSms(string $to, string $message): array {
$auth = new LoginAuthentication(
config('esendex.account_ref'),
config('esendex.username'),
config('esendex.password')
);
$service = new DispatchService($auth);
$dispatch = new DispatchMessage(
config('esendex.default_sender'), // e.g., "YourApp"
$to,
$message,
\Esendex\Model\Message::SmsType
);
$result = $service->send($dispatch);
return [
'message_id' => $result->id(),
'uri' => $result->uri(),
];
}
}
Register the service in config/services.php:
'esendex' => [
'account_ref' => env('ESENDEX_ACCOUNT_REF'),
'username' => env('ESENDEX_USERNAME'),
'password' => env('ESENDEX_PASSWORD'),
'default_sender' => env('ESENDEX_DEFAULT_SENDER', 'YourApp'),
],
Trigger from a Controller
use App\Services\EsendexService;
class NotificationController extends Controller {
public function sendWelcomeSms(Request $request) {
$esendex = app(EsendexService::class);
$response = $esendex->sendSms(
$request->phone,
"Welcome to our app! Use code: {$request->code}"
);
return response()->json($response);
}
}
EsendexService) to:
class EsendexService {
protected $auth;
protected $dispatchService;
protected $inboxService;
public function __construct() {
$this->auth = new LoginAuthentication(
config('esendex.account_ref'),
config('esendex.username'),
config('esendex.password')
);
$this->dispatchService = new DispatchService($this->auth);
$this->inboxService = new InboxService($this->auth);
}
// ... methods for sendSms(), fetchInbox(), etc.
}
// Queue the job
SendSmsJob::dispatch($phone, $message);
// Job class
class SendSmsJob implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable;
public function handle() {
$esendex = app(EsendexService::class);
$esendex->sendSms($this->phone, $this->message);
}
}
// app/Console/Commands/FetchInboxMessages.php
class FetchInboxMessages extends Command {
public function handle() {
$esendex = app(EsendexService::class);
$messages = $esendex->fetchInbox();
foreach ($messages as $msg) {
// Process replies (e.g., update DB, trigger webhooks)
Reply::create([
'phone' => $msg->originator(),
'message' => $msg->summary(),
]);
}
}
}
Schedule in app/Console/Kernel.php:
protected function schedule(Schedule $schedule) {
$schedule->command('fetch:inbox')->everyFiveMinutes();
}
// routes/web.php
Route::post('/esendex/webhook', [EsendexWebhookController::class]);
// app/Http/Controllers/EsendexWebhookController.php
class EsendexWebhookController extends Controller {
public function handleWebhook(Request $request) {
$payload = $request->json()->all();
// Validate signature (if using Esendex's webhook auth)
// Update DB or trigger events based on $payload['status']
}
}
retryAfter().class SendSmsJob extends Job {
public function handle() {
try {
$esendex = app(EsendexService::class);
$result = $esendex->sendSms($this->phone, $this->message);
} catch (\Esendex\Exception\EsendexException $e) {
if ($e->getCode() === 400 && $this->attempts() < 3) {
$this->release(60); // Retry after 60 seconds
}
throw $e;
}
}
}
config() or .env for credentials.composer.json to pin the version:
"esendex/sdk": "^3.0"
429 Too Many Requests errors.
Fix:
throttle middleware for API routes calling Esendex.Cache::remember()).send() are not guaranteed to be unique across accounts or time. Use them only for temporary tracking.
Tip: Store a local UUID in your DB alongside the Esendex message_id for reliable reference.DispatchService:
$mockService = Mockery::mock(\Esendex\DispatchService::class);
$mockService->shouldReceive('send')
->once()
->andReturn(new \Esendex\Model\DispatchResult('test-id', 'http://example.com'));
$this->app->instance(\Esendex\DispatchService::class, $mockService);
check-access Phing task (from README) to validate credentials in CI:
vendor/bin/phing check-access
How can I help you explore Laravel packages today?