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.
Installation
composer require yiisoft/yii2-symfonymailer
Add to config/web.php (or console.php for CLI):
'components' => [
'mailer' => [
'class' => 'yiisoft\yii2\symfonymailer\Mailer',
'transport' => [
'dsn' => 'smtp://user:pass@smtp.example.com:587',
// OR for API-based (e.g., SendGrid):
// 'dsn' => 'sendgrid://api_key@default',
],
],
],
First Use Case Send a basic email in a controller:
use yii\base\View;
use yii\mail\BaseMailer;
public function actionSendWelcome()
{
Yii::$app->mailer->compose()
->setTo('user@example.com')
->setSubject('Welcome!')
->setTextBody('Thanks for signing up!')
->send();
}
Key Files to Review
vendor/yiisoft/yii2-symfonymailer/src/Mailer.php (core class)config/mail.php (Symfony Mailer config reference)Use environment variables or config to toggle transports (e.g., dev/staging/prod):
'transport' => [
'dsn' => getenv('MAILER_DSN') ?: 'smtp://localhost',
],
Leverage Symfony’s EmbeddedFile for CSS/JS:
$message = (new \Symfony\Component\Mime\Email())
->html(Yii::$app->view->render('emails/welcome', ['user' => $user]))
->embedFromDir(__DIR__ . '/assets', '.css');
Yii::$app->mailer->send($message);
Use Symfony’s MailerInterface with a queue transport (e.g., doctrine://default):
'transport' => [
'dsn' => 'doctrine://default',
],
Note: Requires Symfony Messenger integration.
$message->attachFromPath(__DIR__ . '/file.pdf');
// OR for dynamic content:
$message->attach(new \Symfony\Component\Mime\Part\StringPart(
file_get_contents($filePath),
'application/pdf'
));
Use the null transport for tests:
'transport' => ['dsn' => 'null://default'],
Capture sent emails with:
$mailer = Yii::$app->mailer;
$sent = $mailer->send($message);
$this->assertCount(1, $mailer->getSentMessages());
Yii::$app->view to render email templates (e.g., emails/welcome.php).yii\mail\MessageEvent for pre-send modifications:
Yii::$app->mailer->on(\yii\mail\MessageEvent::EVENT_BEFORE_SEND, function ($event) {
$event->message->getHeaders()->addTextHeader('X-Custom', 'value');
});
smtp://?retries=3).mailgun://, sendgrid://, or postmark:// for cloud providers.'transport' => [
'dsn' => 'smtp://user:pass@smtp.example.com:587',
'options' => [
'connection_pool' => true,
'max_connections' => 5,
],
],
DSN Format Strictness
//) throw cryptic errors. Validate with:
use Symfony\Component\Mailer\Transport\Dsn;
$dsn = new Dsn('smtp://user:pass@host:port');
smtp://user%40domain:pass@host.? for options: smtp://?encryption=ssl&auth_mode=login.Attachment Encoding
EmbeddedFile or StringPart with proper MIME types.Yii 2.x Mailer Compatibility
yii\mail\BaseMailer interface. Some methods (e.g., sendMultiple()) may behave differently. Check the source for supported methods.Queue Transports
doctrine:// requires Symfony Messenger. For Yii-only projects, use amqp:// or redis:// with a queue worker.HTML Email Quirks
Email class escapes HTML by default. For raw HTML, use:
$message->html($html, 'text/html', ['charset' => 'UTF-8']);
Enable Symfony Mailer Debug Add to config:
'transport' => [
'dsn' => 'smtp://user:pass@host?debug=1',
],
Logs appear in Symfony’s debug toolbar or var/log/dev.log.
SMTP Debugging Use a local SMTP server (e.g., MailHog) for testing:
'transport' => [
'dsn' => 'smtp://mailhog:1025',
],
Access MailHog at http://localhost:8025.
Common Errors
auth_mode (e.g., ?auth_mode=cram-md5).addTextHeader() for custom headers, not add().Environment-Specific Configs
Use config/params.php or environment variables to manage transports:
'mailer' => [
'class' => 'yiisoft\yii2\symfonymailer\Mailer',
'transport' => ['dsn' => Yii::$app->params['mailer_dsn']],
],
Template Inheritance
Extend Yii’s View for email layouts:
// views/layouts/email.php
<html>
<body>
<?php echo $content; ?>
<footer>© <?php echo date('Y'); ?></footer>
</body>
</html>
Rate Limiting For API transports (e.g., SendGrid), handle rate limits with retries:
'transport' => [
'dsn' => 'sendgrid://key@default',
'options' => [
'max_retries' => 3,
'retry_delay' => 1000, // ms
],
],
Custom Transports
Extend yiisoft\yii2\symfonymailer\Mailer to add proprietary transports:
class CustomMailer extends \yiisoft\yii2\symfonymailer\Mailer {
public function getTransport() {
return new \Symfony\Component\Mailer\Transport\CustomTransport();
}
}
Localization
Use Symfony’s Email with locale-aware headers:
$message->getHeaders()->addTextHeader('Content-Language', 'en-US');
How can I help you explore Laravel packages today?