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

Swiftmailer Bundle Laravel Package

94noni/swiftmailer-bundle

Fork of Symfony Swiftmailer Bundle with Symfony 6+ support (tagged v4). Intended for personal projects needing Swiftmailer, especially for sending emails from CLI commands where Symfony Mailer may not work for specific use cases.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Install the Package Add the package to your composer.json:

    composer require 94noni/swiftmailer-bundle
    

    Ensure your project uses Symfony 6+ and PHP 8.1+.

  2. Configure Swiftmailer Update your config/packages/swiftmailer.yaml (or equivalent Laravel config) to match Symfony’s Swiftmailer setup:

    framework:
        mailer:
            dsn: '%env(MAILER_DSN)%'
            # Example: 'smtp://user:pass@smtp.example.com:port'
    
  3. First Use Case: Sending Emails from CLI Use Symfony’s Mailer component directly in a Laravel Artisan command:

    use Symfony\Component\Mailer\MailerInterface;
    use Symfony\Component\Mime\Email;
    
    class SendTestEmailCommand extends Command
    {
        protected static $defaultName = 'app:send-test-email';
    
        public function handle(MailerInterface $mailer)
        {
            $email = (new Email())
                ->from('sender@example.com')
                ->to('recipient@example.com')
                ->subject('Test Email')
                ->text('Hello from Swiftmailer!');
    
            $mailer->send($email);
            $this->info('Email sent!');
        }
    }
    

    Register the command in app/Console/Kernel.php:

    protected $commands = [
        \App\Console\Commands\SendTestEmailCommand::class,
    ];
    
  4. Verify Configuration Run the command to test:

    php artisan app:send-test-email
    

Implementation Patterns

Usage Patterns

  1. Dependency Injection (DI) Integration Leverage Symfony’s DI container in Laravel by binding services explicitly or using autowiring:

    // In a Laravel service provider (e.g., AppServiceProvider)
    $this->app->bind(MailerInterface::class, function ($app) {
        return new Mailer(
            new Transport($app['config']['mailer.dsn'])
        );
    });
    
  2. Reusable Email Templates Create reusable email templates using Symfony’s Email class:

    // app/Services/EmailService.php
    class EmailService
    {
        public function sendWelcomeEmail(MailerInterface $mailer, string $email, string $name)
        {
            $email = (new Email())
                ->from('noreply@example.com')
                ->to($email)
                ->subject("Welcome, $name!")
                ->html($this->renderTemplate('welcome.html.twig', ['name' => $name]));
    
            $mailer->send($email);
        }
    
        private function renderTemplate(string $template, array $data): string
        {
            // Use Twig or Laravel Blade to render templates
            return view($template, $data)->render();
        }
    }
    
  3. Async Email Processing Use Symfony’s AsyncMailer for background email sending (requires a message queue like Symfony Messenger):

    # config/packages/messenger.yaml
    framework:
        messenger:
            transports:
                async: '%env(MESSENGER_TRANSPORT_DSN)%'
            routing:
                'Symfony\Component\Mailer\Messenger\SendEmailMessage': async
    
  4. Testing Emails Mock the MailerInterface in PHPUnit tests:

    use Symfony\Component\Mailer\MailerInterface;
    use Symfony\Component\Mime\Email;
    
    public function testSendEmail()
    {
        $mailer = $this->createMock(MailerInterface::class);
        $mailer->expects($this->once())
            ->method('send')
            ->with($this->isInstanceOf(Email::class));
    
        $this->app->instance(MailerInterface::class, $mailer);
    
        // Test your email-sending logic here
    }
    

Workflows

  1. CLI-Driven Email Workflows Use Artisan commands for bulk email operations (e.g., newsletters):

    class SendNewsletterCommand extends Command
    {
        protected static $defaultName = 'app:send-newsletter';
    
        public function handle(MailerInterface $mailer, UserRepository $users)
        {
            $users->all()->each(function ($user) use ($mailer) {
                $mailer->send($this->createNewsletterEmail($user));
            });
        }
    
        private function createNewsletterEmail(User $user): Email
        {
            // ...
        }
    }
    
  2. Event-Driven Emails Trigger emails from Laravel events (e.g., user registration):

    // In an event listener
    public function handle(UserRegistered $event)
    {
        $mailer = app(MailerInterface::class);
        $mailer->send($this->createWelcomeEmail($event->user));
    }
    
  3. Attachment Handling Attach files dynamically:

    $email = (new Email())
        ->attachFromPath('/path/to/file.pdf')
        ->embed($this->mailer->embedFromPath('/path/to/image.png'))
        ->html('Check the <img src="cid:image.png"> and <a href="cid:file.pdf">attachment</a>.');
    

Integration Tips

  1. Laravel-Symfony Hybrid Projects If your project mixes Laravel and Symfony, ensure consistent configuration:

    • Use Symfony’s Mailer component as the single source of truth for email logic.
    • Avoid duplicating email-related logic between Laravel’s Mail facade and Symfony’s Mailer.
  2. Queue Integration For Laravel queue workers, ensure Symfony’s AsyncMailer is properly configured with a queue adapter (e.g., Redis, Doctrine):

    # config/packages/messenger.yaml
    framework:
        messenger:
            transports:
                async: 'doctrine://default'
            routing:
                'Symfony\Component\Mailer\Messenger\SendEmailMessage': async
    
  3. Logging and Debugging Enable Symfony’s mail logging for debugging:

    # config/packages/monolog.yaml
    monolog:
        handlers:
            mail:
                type: stream
                path: '%kernel.logs_dir%/%kernel.environment%.mail.log'
                level: debug
                channels: ['mailer']
    
  4. Environment-Specific Configs Use Laravel’s environment configs to switch between development/staging/production:

    # .env
    MAILER_DSN=smtp://user:pass@smtp.example.com:587?encryption=tls&authmode=login
    
    # config/packages/swiftmailer.yaml
    framework:
        mailer:
            dsn: '%env(MAILER_DSN)%'
    

Gotchas and Tips

Pitfalls

  1. Symfony 6+ Breaking Changes

    • DI Container: Symfony 6 uses a stricter autowiring system. Explicit bindings may be required for custom services.
      // Example: Explicit binding for a custom transport
      $this->app->bind(TransportInterface::class, function () {
          return new DsnTransport('%env(MAILER_DSN)%');
      });
      
    • Deprecated Features: Some Swiftmailer features (e.g., Swift_Events) may be removed. Update to Symfony’s Mailer events instead.
  2. PHP 8.1+ Requirements

    • Typed Properties: Ensure your classes use PHP 8.1+ typed properties if leveraging Symfony’s newer features.
    • Union Types: Some Symfony methods now use union types (e.g., array|string), which may cause type errors in older PHP.
  3. Laravel-Specific Quirks

    • Service Provider Conflicts: If using Laravel’s Mail facade alongside Symfony’s Mailer, ensure no duplicate configurations exist.
    • Queue Workers: Symfony’s AsyncMailer may not integrate seamlessly with Laravel’s queue system out of the box. Test thoroughly.
  4. Configuration Overrides

    • Laravel’s config/mail.php may conflict with Symfony’s swiftmailer.yaml. Prioritize Symfony’s config for consistency:
      # config/packages/swiftmailer.yaml
      framework:
          mailer:
              dsn: '%env(MAILER_DSN)%'
              # Override Laravel's mail config here
      
  5. Event Listener Conflicts

    • Symfony’s MailerEvents (e.g., SentEvent) may not trigger Laravel’s MailSent events. Use Symfony’s event system exclusively for mail-related events.

Debugging

  1. Email Not Sending?

    • Check DSN: Verify %env(MAILER_DSN)% is correctly set in .env.
    • Transport Issues: Test the DSN manually:
      php bin/console debug:mailer:transport
      
    • Logs: Enable mail logging (see Integration Tips above) to debug failed sends.
  2. Type Errors in PHP 8.1+

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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui