Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Msg91 Laravel Laravel Package

kaydee123/msg91-laravel

Laravel integration for MSG91 SMS and OTP services. Send single/bulk SMS, template-based messages, OTP send/verify/resend (text/voice), and DLT-ready India compliance. Includes auto-discovery service provider, facade, helpers, and publishable config.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require kaydee123/msg91-laravel
    

    The package auto-registers via Laravel’s auto-discovery.

  2. Configuration: Publish the config file:

    php artisan vendor:publish --tag=msg91-config
    

    Update .env with your MSG91 credentials:

    MSG91_AUTH_KEY=your_auth_key
    
  3. First Use Case: Send a basic SMS via the facade:

    use Kaydee123\Msg91\Facades\Msg91;
    
    Msg91::sendSms('919876543210', 'Hello from Laravel!');
    

Where to Look First

  • Facade: Kaydee123\Msg91\Facades\Msg91 (for quick usage).
  • Config: config/msg91.php (for customization).
  • Helper Methods: Check src/Helpers for utility functions (if any).

Implementation Patterns

Core Workflows

  1. Sending SMS:

    • Single SMS:
      Msg91::sendSms('919876543210', 'Your OTP is 123456');
      
    • Bulk SMS:
      Msg91::sendBulkSms(['919876543210', '919876543211'], 'Hello all!');
      
    • Template-Based SMS (DLT-compliant for India):
      Msg91::sendTemplateSms('919876543210', 'OTP_TEMPLATE_ID', ['otp' => '123456']);
      
  2. OTP Management:

    • Send OTP:
      $otp = Msg91::sendOtp('919876543210', 'OTP_TEMPLATE_ID', ['otp' => '123456']);
      
    • Verify OTP:
      $isValid = Msg91::verifyOtp('919876543210', '123456', $otp->id);
      
    • Resend OTP:
      Msg91::resendOtp($otp->id);
      
  3. Error Handling: Wrap calls in try-catch to handle exceptions (e.g., Msg91Exception):

    try {
        Msg91::sendSms('919876543210', 'Test');
    } catch (\Kaydee123\Msg91\Exceptions\Msg91Exception $e) {
        Log::error('MSG91 Error: ' . $e->getMessage());
    }
    

Integration Tips

  • Queue SMS for Background Processing: Use Laravel Queues to avoid blocking requests:
    Msg91::queueSms('919876543210', 'Hello', 'default');
    
  • Logging: Enable debug mode in .env (MSG91_DEBUG=true) for API response logging.
  • Testing: Use mock responses in PHPUnit:
    $this->mock(Msg91::class)->shouldReceive('sendSms')->andReturn(true);
    

Gotchas and Tips

Pitfalls

  1. Authentication Errors:

    • Issue: 401 Unauthorized if MSG91_AUTH_KEY is invalid or expired.
    • Fix: Regenerate the key in MSG91 Dashboard and update .env.
  2. Rate Limits:

    • MSG91 enforces rate limits. Handle 429 Too Many Requests by:
      • Implementing exponential backoff.
      • Using Msg91::retry() helper if available.
  3. DLT Compliance (India):

    • Issue: Template-based SMS must use registered templates for DLT compliance.
    • Fix: Register templates in MSG91 Dashboard and use sendTemplateSms().
  4. Timeouts:

    • Default timeout is 30s (MSG91_TIMEOUT). Increase if API responses are slow:
      MSG91_TIMEOUT=60
      

Debugging

  • Enable Debug Mode:

    MSG91_DEBUG=true
    

    Logs API requests/responses to storage/logs/msg91.log.

  • Check Response Codes: MSG91 returns HTTP codes (e.g., 200 for success, 400 for invalid params). Refer to MSG91 API Docs for specifics.

Extension Points

  1. Custom Templates: Extend the package by adding template logic in a service class:

    class CustomMsg91Service extends \Kaydee123\Msg91\Msg91Service {
        public function sendCustomTemplate($mobile, $templateData) {
            // Custom logic
        }
    }
    
  2. Event Listeners: Listen for SMS/OTP events (if the package emits them):

    // config/listeners.php
    'Kaydee123\Msg91\Events\SmsSent' => [
        \App\Listeners\LogSmsSent::class,
    ],
    
  3. Override Config: Extend the published config file (config/msg91.php) to add custom settings:

    'custom' => [
        'prefix' => '+91',
    ],
    

Pro Tips

  • Batch Processing: Use sendBulkSms() for cost efficiency when sending to multiple numbers.
  • OTP Expiry: MSG91 OTPs expire after 10 minutes by default. Adjust in the template settings.
  • Webhooks: Configure MSG91 webhooks for real-time delivery reports (requires MSG91 Dashboard setup).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony