ems-spot/laravel-melipayamak-sms
Unofficial Laravel 5 notification channel for Melipayamak SMS. Configure credentials via .env, publish the package config, then send messages with SMS->text()->to()->sendText() and return EmsSpot\Melipayamak\SMS in via().
Installation:
composer require ems-spot/laravel-melipayamak-sms
php artisan vendor:publish --provider="EmsSpot\Melipayamak\MelipayamakServiceProvider"
.env with your Melipayamak credentials:
MELIPAYAMAK_USERNAME=your_username
MELIPAYAMAK_PASSWORD=your_password
MELIPAYAMAK_FROM=your_sender_id
MELIPAYAMAK_DEBUG=false
MELIPAYAMAK_DEBUG_RECIPIENT=test_phone
First Use Case:
Notifiable trait in your user model:
use Notifiable;
use EmsSpot\Melipayamak\SMS;
via() and toSms() methods in your notifiable class (e.g., User):
public function via($notifiable)
{
return ['EmsSpot\Melipayamak\SMS'];
}
public function toSms($notifiable)
{
return (new SMS)
->text('Your activation code is: ' . $this->activation_code)
->to($notifiable->phone)
->sendText();
}
$user->notify(new YourNotificationClass());
Basic SMS Notification:
SMS facade or class directly in your notifiable classes:
(new SMS)
->text('Hello, this is a test message.')
->to('+905551234567')
->sendText();
Dynamic Content:
public function toSms($notifiable)
{
return (new SMS)
->text(__('sms.welcome', ['name' => $notifiable->name]))
->to($notifiable->phone)
->sendText();
}
Debugging Mode:
.env to test messages without sending:
MELIPAYAMAK_DEBUG=true
MELIPAYAMAK_DEBUG_RECIPIENT=+905551234567
Batch Sending:
foreach ($users as $user) {
$user->notify(new YourNotificationClass());
}
Customizing the SMS Class:
EmsSpot\Melipayamak\SMS class to add custom methods or logic:
class CustomSMS extends SMS
{
public function withPriority($priority)
{
$this->priority = $priority;
return $this;
}
}
User Activation:
$user->activation_code = Str::random(6);
$user->notify(new ActivationNotification($user));
Password Reset:
$user->notify(new PasswordResetNotification($user));
Transactional Alerts:
$order->user->notify(new OrderStatusUpdatedNotification($order));
Queue Notifications:
$user->notify(new YourNotificationClass())->onQueue('sms');
Logging:
use Illuminate\Support\Facades\Log;
public function toSms($notifiable)
{
$sms = (new SMS)
->text('Your message.')
->to($notifiable->phone);
$response = $sms->sendText();
Log::info('SMS sent to ' . $notifiable->phone, ['response' => $response]);
return $sms;
}
Testing:
$this->mock(EmsSpot\Melipayamak\SMS::class)
->shouldReceive('sendText')
->once()
->andReturn(true);
Environment Configuration:
MELIPAYAMAK_FROM (sender ID) will cause the SMS to fail silently. Always validate this field.Phone Number Format:
+905551234567). Ensure your database stores numbers in this format or normalize them before sending:
$phone = '+90' . str_replace('0', '', $notifiable->phone);
Debug Mode Misuse:
MELIPAYAMAK_DEBUG_RECIPIENT instead of the intended recipient. Remember to disable it (MELIPAYAMAK_DEBUG=false) before production.Rate Limits:
MelipayamakException in your code:
try {
$sms->sendText();
} catch (\Exception $e) {
Log::error('Failed to send SMS: ' . $e->getMessage());
// Retry or notify admin
}
Character Limits:
Check Response:
sendText() method returns a response object. Inspect it for errors:
$response = $sms->sendText();
if ($response->success === false) {
// Handle error
}
Enable Debug Logging:
MELIPAYAMAK_DEBUG=true and check Laravel logs for debug messages.Test with Known Working Credentials:
MELIPAYAMAK_USERNAME and MELIPAYAMak_PASSWORD are correct.Config File Location:
config/melipayamak.php. Customize it as needed:
'default' => [
'username' => env('MELIPAYAMAK_USERNAME'),
'password' => env('MELIPAYAMAK_PASSWORD'),
'from' => env('MELIPAYAMAK_FROM'),
'debug' => env('MELIPAYAMAK_DEBUG', false),
'debug_recipient' => env('MELIPAYAMAK_DEBUG_RECIPIENT'),
],
Multiple Configurations:
config() helper to switch:
$sms = (new SMS)->setConfig('client2');
Custom SMS Class:
EmsSpot\Melipayamak\SMS class in your AppServiceProvider:
public function register()
{
$this->app->bind('EmsSpot\Melipayamak\SMS', function () {
return new App\Services\CustomSMS();
});
}
Add Custom Methods:
SMS class to add features like scheduling or priority:
class CustomSMS extends SMS
{
public function schedule(\Carbon\Carbon $time)
{
$this->scheduledAt = $time;
return $this;
}
}
Event Listeners:
SMSSent) to log or process responses:
Event::listen('EmsSpot\Melipayamak\Events\SMSSent', function ($event) {
Log::info('SMS sent to ' . $event->phone, ['message' => $event->message]);
});
Middleware:
$sms->through(function ($notifiable, $sms) {
if (!$notifiable->canReceiveSMS()) {
throw new \Exception('User cannot receive SMS.');
}
return $sms;
});
How can I help you explore Laravel packages today?