yiisoft/yii2-swiftmailer
SwiftMailer integration for Yii2 apps. Send emails via SMTP, sendmail or third-party transports with simple configuration, templated messages, and Yii2 mailer component support for composing and delivering HTML/text mail reliably across environments.
Installation:
composer require yiisoft/yii2-swiftmailer
Add to your config/web.php (or console.php for CLI emails):
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false, // Set to true for debugging
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.example.com',
'username' => 'user@example.com',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
],
First Use Case: Send a simple email in a controller:
Yii::$app->mailer->compose()
->setTo('recipient@example.com')
->setSubject('Hello!')
->setTextBody('This is a test email.')
->send();
Where to Look First:
vendor/yiisoft/yii2-swiftmailer/src/Mailer.php for core logic.Dynamic Email Templates:
Use compose() with views for reusable templates:
Yii::$app->mailer->compose('emails/welcome', ['user' => $user])
->setFrom(['noreply@example.com' => 'Example'])
->setTo($user->email)
->setSubject('Welcome!')
->send();
Attachments:
$message = Yii::$app->mailer->compose()
->setFrom(['from@example.com' => 'From'])
->setTo('to@example.com')
->setSubject('With Attachment')
->attach(Yii::getAlias('@webroot/files/report.pdf'));
$message->send();
HTML + Plaintext Emails:
Yii::$app->mailer->compose()
->setFrom(['from@example.com' => 'From'])
->setTo('to@example.com')
->setSubject('HTML Email')
->setHtmlBody('<b>HTML Content</b>')
->setTextBody('Plaintext fallback')
->send();
Queueing Emails (with yii2-queue):
Yii::$app->mailer->compose()
->setTo('recipient@example.com')
->setSubject('Queued Email')
->setTextBody('This will be sent later.')
->queue(); // Uses Yii's queue system
Customizing Headers:
$message = Yii::$app->mailer->compose();
$message->getHeaders()->addTextHeader('X-Custom-Header', 'value');
$message->send();
EmailValidator) to validate email addresses before sending.useFileTransport in development to log emails to runtime/mail..env and load them via Yii::$app->params.SMTP Connection Issues:
host, port, encryption (e.g., tls/ssl), and credentials. Test with useFileTransport first.'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'logging' => true, // Enable SwiftMailer logging
],
],
],
HTML Emails Rendering Incorrectly:
Queue Not Working:
queue() method.yii2-queue is installed and configured. Use send() instead of queue() for immediate delivery.Character Encoding Issues:
Yii::$app->mailer->compose()
->setCharSet('UTF-8')
->setSubject('Subject with UTF-8: café')
->send();
Memory Limits:
memory_limit.memory_limit in php.ini or optimize attachments (e.g., compress files).Reusable Components:
Create a MailService class to encapsulate common logic:
class MailService {
public function sendWelcomeEmail($user) {
Yii::$app->mailer->compose(['html' => 'emails/welcome.html', 'text' => 'emails/welcome.txt'], ['user' => $user])
->setFrom(['noreply@example.com' => 'Example'])
->setTo($user->email)
->setSubject('Welcome!')
->send();
}
}
Testing:
useFileTransport to test emails without sending:
'mailer' => [
'useFileTransport' => true,
],
runtime/mail.Performance:
Swift_EventDispatcher to optimize transport (e.g., connection pooling).Security:
.env or secure vaults.$message->setSubject(Yii::$app->security->encode(Yii::$app->request->post('subject')));
Extending SwiftMailer:
Mailer class to add custom methods:
class CustomMailer extends \yii\swiftmailer\Mailer {
public function sendTransactional($view, $model) {
// Custom logic
}
}
config/web.php:
'mailer' => ['class' => 'app\mail\CustomMailer'],
Debugging:
'transport' => [
'debug' => true,
],
Swift_TransportException).How can I help you explore Laravel packages today?