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

Fake Sms Notifier Laravel Package

symfony/fake-sms-notifier

Symfony Notifier transport that fakes SMS delivery during development. Redirect SMS messages to email (with configurable to/from and optional custom mailer transport) or log them via a logger DSN, without sending real texts.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup for Laravel

  1. Install Dependencies:

    composer require symfony/notifier symfony/fake-sms-notifier
    

    Note: Laravel does not natively use Symfony Notifier, so this requires a bridge (see Implementation Patterns).

  2. Configure DSN in .env:

    # Option 1: Fake SMS as email (sent to your inbox)
    FAKE_SMS_DSN=fakesms+email://default?to=dev@example.com&from=TestSMS
    
    # Option 2: Fake SMS as logs (visible in Laravel logs)
    FAKE_SMS_DSN=fakesms+logger://default
    
  3. First Use Case: Testing SMS Notifications Create a test notification class (if using Laravel Notifications) or directly use Symfony Notifier:

    // Example using Symfony Notifier (requires bridge)
    use Symfony\Component\Notifier\Notifier;
    use Symfony\Component\Notifier\Message\SmsMessage;
    
    $notifier = app(Notifier::class);
    $notifier->send(new SmsMessage('Hello from fake SMS!', '+1234567890'));
    

    Output: Check your email inbox or Laravel logs (storage/logs/laravel.log) for the fake SMS.


Implementation Patterns

1. Laravel-Symfony Notifier Bridge

Since Laravel doesn’t natively support Symfony Notifier, create a service provider to integrate them:

// app/Providers/SymfonyNotifierServiceProvider.php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Transport\FakeSmsTransportFactory;

class SymfonyNotifierServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(Notifier::class, function ($app) {
            $dsn = config('services.fake_sms.dsn');
            $factory = new FakeSmsTransportFactory();
            return new Notifier([$factory->createTransport($dsn)]);
        });
    }
}

Register in config/app.php:

'providers' => [
    App\Providers\SymfonyNotifierServiceProvider::class,
],

2. Dynamic Environment-Based Switching

Use Laravel’s config to toggle between fake and real SMS transports:

// config/services.php
'fake_sms' => [
    'dsn' => env('FAKE_SMS_DSN', 'fakesms+logger://default'),
],

Example Usage:

$notifier = app(Notifier::class);
$notifier->send(new SmsMessage('Test', '+1234567890'));

3. Laravel Notification Channel Adapter

Extend Laravel’s Notification system to support fake SMS:

// app/Notifications/FakeSmsChannel.php
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Symfony\Component\Notifier\Notifier;

class FakeSmsChannel
{
    public function __construct(private Notifier $notifier) {}

    public function send($notifiable, string $message)
    {
        $this->notifier->send(new \Symfony\Component\Notifier\Message\SmsMessage($message, $notifiable->phone));
    }
}

Usage in Notification:

// app/Notifications/SmsVerification.php
use App\Notifications\FakeSmsChannel;

public function via($notifiable)
{
    return [new FakeSmsChannel(app(Notifier::class))];
}

4. Testing Workflow

Use fake SMS in tests to verify notifications:

// tests/Feature/SmsNotificationTest.php
use Symfony\Component\Notifier\Message\SmsMessage;

public function test_sms_notification()
{
    $notifier = app(Notifier::class);
    $notifier->send(new SmsMessage('Test', '+1234567890'));

    // Assert via email (if using fakesms+email)
    $this->assertDatabaseHas('notifications', [
        'message' => 'Test',
    ]);
    // OR check logs for fake SMS output.
}

5. CI/CD Integration

Configure GitHub Actions or other CI tools to use fake SMS for deterministic testing:

# .github/workflows/test.yml
env:
    FAKE_SMS_DSN: fakesms+logger://default

Gotchas and Tips

Pitfalls

  1. Symfony Notifier Dependency Overhead

    • Adding symfony/notifier (~50+ dependencies) may conflict with existing Laravel packages or increase bundle size.
    • Fix: Use a composer script to isolate dependencies or evaluate if the package is worth the overhead for your use case.
  2. Laravel-Symfony Integration Complexity

    • Laravel’s Notification system and Symfony Notifier are not drop-in compatible. Expect boilerplate code for bridging.
    • Fix: Abstract the bridge logic into a service class to avoid repetition.
  3. Log Clutter with fakesms+logger

    • Fake SMS logs may mix with Laravel’s default logs, making debugging harder.
    • Fix: Configure a dedicated log channel for fake SMS:
      // config/logging.php
      'channels' => [
          'fake_sms' => [
              'driver' => 'single',
              'path' => storage_path('logs/fake_sms.log'),
              'level' => 'debug',
          ],
      ],
      
      Then update the DSN to use a custom logger:
      FAKE_SMS_DSN=fakesms+logger://fake_sms
      
  4. Email Routing Issues with fakesms+email

    • If using fakesms+email, ensure the to and from addresses in the DSN are valid and accessible.
    • Fix: Use a team alias (e.g., dev-team+fakesms@example.com) to aggregate fake SMS emails.
  5. Phone Number Formatting

    • Symfony Notifier expects phone numbers in E.164 format (e.g., +1234567890). Malformed numbers may cause silent failures.
    • Fix: Validate phone numbers before sending:
      use Symfony\Component\Notifier\Bridge\FakeSms\PhoneNumber;
      
      $phone = new PhoneNumber('+1234567890'); // Valid
      // $phone = new PhoneNumber('1234567890'); // Invalid (missing '+')
      
  6. No Support for Attachments or Rich Media

    • Fake SMS only supports plain text. If your app uses MMS or rich media, this package won’t simulate those.
    • Fix: Use a custom transport or combine with other mocking tools.

Debugging Tips

  1. Enable Symfony Notifier Debug Mode Add this to your config/services.php to log transport creation:

    'services' => [
        'notifier' => [
            'debug' => env('APP_DEBUG', false),
        ],
    ],
    
  2. Inspect Fake SMS Transport Dump the transport object to verify configuration:

    $transport = (new FakeSmsTransportFactory())->createTransport(env('FAKE_SMS_DSN'));
    dump($transport);
    
  3. Check for Deprecation Warnings Some older versions of Symfony Notifier may trigger deprecation notices. Update to the latest stable version:

    composer require symfony/notifier:^7.0
    

Extension Points

  1. Custom Transport Backend Extend the package to support additional backends (e.g., Slack, database):

    // app/Notifier/FakeSms/SlackTransport.php
    namespace App\Notifier\FakeSms;
    
    use Symfony\Component\Notifier\Transport\FakeSmsTransportInterface;
    
    class SlackTransport implements FakeSmsTransportInterface
    {
        public function send(SmsMessage $message)
        {
            // Send to Slack webhook
            file_get_contents('https://hooks.slack.com/...', json_encode([
                'text' => $message->getContent(),
            ]));
        }
    }
    

    Register it in your Notifier instance.

  2. Override Default Behavior Use Symfony’s event system to intercept fake SMS messages:

    // app/Providers/EventServiceProvider.php
    use Symfony\Component\Notifier\EventListener\TransportListener;
    
    protected function shouldDispatch(): array
    {
        return [
            TransportListener::class,
        ];
    }
    
  3. Laravel Horizon Integration If using queues, ensure fake SMS transports are serializable for queue jobs:

    // app/Providers/AppServiceProvider.php
    use Illuminate\Contracts\Queue\ShouldQueue;
    
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle