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 Bridge Laravel Package

symfony/swiftmailer-bridge

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup in Laravel

  1. Installation Add the package via Composer (though note this is a Symfony bridge, not a standalone Laravel package):

    composer require symfony/swiftmailer-bridge
    

    Note: In Laravel, SwiftMailer is typically used via the swiftmailer/swiftmailer package (or its successor, symfony/mailer). This bridge is primarily for Symfony apps, but Laravel can still leverage it for integration with Symfony components like HttpClient or Process.

  2. First Use Case: Sending Emails with Symfony’s Mailer If you’re migrating from SwiftMailer to Symfony’s Mailer (recommended), replace:

    // Old SwiftMailer (deprecated)
    $mailer = new \Swift_Mailer($transport);
    

    With Symfony’s Mailer (which internally uses SwiftMailer):

    // Laravel's built-in Mail facade (already uses Symfony Mailer under the hood)
    use Illuminate\Support\Facades\Mail;
    Mail::raw('Email content', function ($message) {
        $message->to('user@example.com')
                ->subject('Test Email');
    });
    
  3. Where to Look First


Implementation Patterns

1. Integration with Symfony Components

  • HttpClient for Email Attachments: Use the bridge to fetch email attachments dynamically via Symfony’s HttpClient:

    use Symfony\Component\Mailer\MailerInterface;
    use Symfony\Contracts\HttpClient\HttpClientInterface;
    
    $mailer = new Mailer(new Transport(), new HttpClient());
    $email = (new Email())
        ->from('sender@example.com')
        ->to('recipient@example.com')
        ->subject('Attachment Example')
        ->html('<p>Check the attachment!</p>')
        ->attachFromUrl(
            $mailer->getTransport()->getUrl(),
            'report.pdf',
            'application/pdf'
        );
    $mailer->send($email);
    

    Laravel Note: Use Laravel’s Http facade instead for simplicity:

    use Illuminate\Support\Facades\Http;
    $attachment = Http::get($url)->body();
    
  • Process for Async Email Handling: Offload email sending to a background process (e.g., using Symfony’s Process):

    use Symfony\Component\Process\Process;
    
    $process = new Process(['php', 'artisan', 'queue:work', '--once']);
    $process->start();
    

2. Laravel-Specific Workflows

  • Custom Mailables with Symfony Features: Extend Laravel’s Mailable to use Symfony’s Email class for advanced features:

    use Symfony\Component\Mime\Email;
    use Illuminate\Mail\Mailable;
    
    class CustomMailable extends Mailable
    {
        public function build()
        {
            $email = (new Email())
                ->from('sender@example.com')
                ->to($this->recipient)
                ->subject('Custom Email')
                ->html($this->template);
    
            return $this->withSwiftMessage(
                $this->swiftMailer->createMessage()
                    ->setFrom($email->getFrom())
                    ->setTo($email->getTo())
                    ->setSubject($email->getSubject())
                    ->setBody($email->getHtmlBody())
            );
        }
    }
    
  • Testing with Symfony’s Mailer: Use Symfony’s TestMailer for isolated email testing:

    use Symfony\Component\Mailer\TestMailer;
    
    $testMailer = new TestMailer();
    $mailer = new Mailer(new Transport(), $testMailer);
    $mailer->send($email);
    
    $emails = $testMailer->getMessages();
    $this->assertCount(1, $emails);
    

3. Transport Configuration

  • Shared Config Between Laravel and Symfony: Define transports in config/mail.php (Laravel) and reuse them in Symfony’s Dsn:
    // Laravel config/mail.php
    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST'),
            'port' => env('MAIL_PORT'),
        ],
    ],
    
    // Symfony Dsn (if needed)
    $transport = new Transport(
        (new Dsn('smtp://user:pass@example.com:1025'))
    );
    

Gotchas and Tips

Pitfalls

  1. Archived Package:

    • This package is archived and primarily for Symfony apps. Laravel already uses Symfony’s Mailer under the hood (via swiftmailer/swiftmailer or symfony/mailer). Avoid reinventing the wheel—use Laravel’s built-in Mail facade or Symfony’s Mailer directly.
  2. Deprecated SwiftMailer:

    • SwiftMailer is deprecated in favor of Symfony’s Mailer. If you’re starting a new project, use:
      composer require symfony/mailer
      
      Laravel’s Mail facade already supports this.
  3. Bridge Overhead:

    • The bridge adds minimal value in Laravel. Focus on:
      • Laravel’s Mail facade for simplicity.
      • Symfony’s Mailer for advanced features (e.g., Email class, TestMailer).

Debugging Tips

  1. Log Emails: Use Laravel’s Mail::pretend() or Symfony’s TestMailer to inspect emails without sending:

    // Laravel
    Mail::pretend();
    Mail::to('user@example.com')->send(new CustomMailable());
    
    // Symfony
    $testMailer = new TestMailer();
    $mailer->send($email);
    dd($testMailer->getMessages());
    
  2. Transport Issues:

    • If emails fail silently, check Laravel’s storage/logs/laravel.log or Symfony’s Monolog handler.
    • For SMTP, verify credentials in .env:
      MAIL_MAILER=smtp
      MAIL_HOST=smtp.example.com
      MAIL_PORT=587
      MAIL_USERNAME=...
      MAIL_PASSWORD=...
      MAIL_ENCRYPTION=tls
      

Extension Points

  1. Custom Swift Events: Laravel’s Mail facade supports events (sending, sent, failed). Extend them for logging or analytics:

    Mail::sending(function ($event) {
        Log::debug('Sending email to: ' . $event->message->getTo()[0]['address']);
    });
    
  2. Symfony’s Email Class: Use Symfony’s Email class for complex emails (e.g., embedded images, custom headers):

    $email = (new Email())
        ->from('newsletter@example.com')
        ->to('user@example.com')
        ->subject('Weekly News')
        ->html($this->renderView('emails/newsletter', ['data' => $data]))
        ->embedFromPath($this->logoPath, 'logo_id');
    
  3. Queueing Emails: Laravel’s Mail facade queues emails by default. For Symfony’s Mailer, use:

    $mailer->send($email); // Synchronous
    // OR use a queue (e.g., Symfony Messenger or Laravel Queues)
    $message = new SendEmailMessage($email);
    $bus->dispatch($message);
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor