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

symfony/swiftmailer-bundle

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require symfony/swiftmailer-bundle
    

    Add the bundle to config/bundles.php (Symfony) or config/app.php (Laravel via bridge):

    return [
        // ...
        Symfony\Swiftmailer\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
    ];
    
  2. Configuration: Edit .env for mail settings (Laravel-style):

    MAIL_MAILER=smtp
    MAIL_HOST=smtp.example.com
    MAIL_PORT=587
    MAIL_USERNAME=your_username
    MAIL_PASSWORD=your_password
    MAIL_ENCRYPTION=tls
    MAIL_FROM_ADDRESS=from@example.com
    MAIL_FROM_NAME="Your App"
    
  3. First Email: Inject Swift_Mailer into a controller/service:

    use Symfony\Bridge\Twig\Mime\TemplatedEmail;
    use Symfony\Component\Mailer\MailerInterface;
    
    public function sendWelcomeEmail(MailerInterface $mailer)
    {
        $email = (new TemplatedEmail())
            ->from('from@example.com')
            ->to('user@example.com')
            ->subject('Welcome!')
            ->htmlTemplate('emails/welcome.html.twig');
    
        $mailer->send($email);
    }
    
  4. Templates: Place Twig templates in resources/views/emails/ (Laravel) or templates/emails/ (Symfony).


Implementation Patterns

Common Workflows

  1. Dynamic Emails: Pass data to templates:

    $email->htmlTemplate('emails/invoice.html.twig')
          ->context(['invoice' => $invoiceData]);
    
  2. Attachments:

    $email->attachFromPath('/path/to/file.pdf');
    // Or inline:
    $email->embed($mailer->getSymfonyMailer()->getTransport()->getUrl(), 'cid:unique_id');
    
  3. Queueing Emails (Laravel): Use Mail::to()->send() with Laravel’s queue system:

    Mail::to('user@example.com')->send(new WelcomeEmail($user));
    
  4. Testing: Use Symfony’s TestMailer or Laravel’s MailFake:

    $mailer = $this->getMailer();
    $sent = $mailer->getSentMessages();
    

Integration Tips

  • Laravel Bridge: Use symfony/mailer + symfony/swiftmailer for modern SwiftMailer features.
  • Event Listeners: Extend Swift_Events_SendEvent for logging/auditing:
    $mailer->getTransport()->getEventDispatcher()->addListener(
        'sent',
        function ($event) { /* Log email */ }
    );
    
  • Spam Avoidance: Set X-Mailer header:
    $email->getHeaders()->addTextHeader('X-Mailer', 'YourApp/1.0');
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warnings:

    • SwiftmailerBundle is archived; prefer symfony/mailer (Symfony 5+) or Laravel’s native mailers.
    • Avoid Swift_* classes; use Symfony\Component\Mailer\* equivalents.
  2. Configuration Overrides:

    • Laravel’s .env may conflict with Symfony’s config/packages/swiftmailer.yaml. Prioritize one source.
  3. TLS/SSL Issues:

    • Debug with:
      php bin/console debug:mailer
      
    • For Laravel, use MAIL_ENCRYPTION=null for plaintext (e.g., local testing).
  4. Template Caching:

    • Clear Twig cache after template changes:
      php artisan view:clear
      

Debugging

  • Enable Verbose Logging:
    # config/packages/swiftmailer.yaml
    swiftmailer:
        logging: true
    
  • Check Sent Emails:
    $mailer->getTransport()->getEventDispatcher()->addListener(
        'sent',
        function ($event) { dd($event->getMessage()); }
    );
    

Extension Points

  1. Custom Transports: Override Swift_Transport for APIs (e.g., SendGrid):

    $transport = new Swift_SmtpTransport('smtp.sendgrid.net', 587, [
        'username' => 'apikey',
        'password' => 'your_key',
        'source_ip' => 'your.ip',
    ]);
    
  2. Message Modifiers: Use Swift_Events_SendEvent to alter messages pre-send:

    $dispatcher->addListener('sent', function ($event) {
        $event->getMessage()->getHeaders()->addTextHeader('X-Custom', 'Value');
    });
    
  3. Laravel-Specific:

    • Use Mail::raw() for non-Twig emails:
      Mail::raw('Plain text', function ($message) {
          $message->to('user@example.com')->subject('Plain Email');
      });
      
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware