yiisoft/yii2-symfonymailer
Yii2 extension integrating Symfony Mailer for reliable email sending. Configure SMTP/DSN transport, templates via viewPath, and file transport for dev. Supports PHP 8.1+ and installs via Composer for seamless Yii 2.0 mail delivery.
Pros:
Mailer component, aligning with modern PHP standards (PSR-compliant, Symfony 6+ support).yii\swiftmailer\Mailer without rewriting core logic, adhering to the Open/Closed Principle.Email class for structured emails (e.g., HTML templates, attachments).Cons:
yii\base\Event) may complicate adoption in greenfield projects or Yii3+.yii\mailer\MailerInterface.yii\swiftmailer\Mailer with minimal config changes (e.g., components.mailer in config/web.php).// Before (SwiftMailer)
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
],
// After (SymfonyMailer)
'mailer' => [
'class' => 'yiisoft\symfonymailer\Mailer',
'transport' => 'smtp://user:pass@smtp.example.com:25',
],
MailerInterface with SymfonyMailer’s MailerInterface.mailer-before-send events.Swift_Events).Event vs. Symfony’s EventDispatcher).symfony/mailer (v6.3+) may conflict with other Symfony packages in the stack.Transport mocking differs from SwiftMailer’s.symfony/http-client for transports).yii\swiftmailer\Mailer in a non-production environment.mailer-before-send).MAILER_DSN=smtp://...).Email class.Swift_Message instances).| Aspect | Compatibility |
|---|---|
| Yii2 Core | High (drop-in replacement for yii\swiftmailer\Mailer). |
| Yii3 | Medium (requires adapter or custom event handling). |
| SwiftMailer Features | Partial (e.g., Swift_Events may not map 1:1 to SymfonyMailer). |
| Symfony Ecosystem | High (works seamlessly with Symfony Messenger, HTTP Client, etc.). |
| PSR Standards | High (PSR-15 MailerInterface, PSR-6 caching if used with Symfony’s cache). |
config/web.php to use yiisoft\symfonymailer\Mailer.'components' => [
'mailer' => [
'class' => 'yiisoft\symfonymailer\Mailer',
'transport' => getenv('MAILER_DSN') ?: 'null://', // Fallback to null transport in dev
],
],
Yii::$app->mailer->compose() with SymfonyMailer’s Email class:
// Before
Yii::$app->mailer->compose()
->setTo('user@example.com')
->setSubject('Hello')
->setTextBody('Plain text')
->send();
// After
$email = (new Email())
->to('user@example.com')
->subject('Hello')
->html('<h1>Hello</h1>');
Yii::$app->mailer->send($email);
Transport for unit tests:
$mailer = new Mailer(new NullTransport());
mailer-before-send) break.symfony/mailer, symfony/mime, etc. (~5MB+).viewRenderer) may need custom logic.Email class vs. SwiftMailer’s dynamic methods.DsnTransport can reuse connections (configurable).How can I help you explore Laravel packages today?