- How do I install yiisoft/yii2-swiftmailer in a Yii2 project?
- Run `composer require yiisoft/yii2-swiftmailer` to install the package. Then configure the mailer component in your Yii2 config file (e.g., `config/web.php`) under the `components` array. The package integrates directly with Yii2’s DI container, so no additional setup is needed beyond SwiftMailer’s transport configuration.
- Does this package support SMTP authentication and TLS/SSL?
- Yes, the package supports SMTP authentication and encryption. Configure the `transport` key in your Yii2 mailer component with `class` set to `Swift_SmtpTransport`, then specify `host`, `username`, `password`, `port`, and `encryption` (e.g., `tls` or `ssl`). Example: `'encryption' => 'tls', 'port' => 587`.
- Can I use HTML templates for emails with this package?
- Yes, you can compose emails with HTML and text bodies using Yii2’s view rendering. Pass a view file path to the `render()` method of the mailer component, or manually set HTML content via `setHtmlBody()`. The package supports attachments and embedded images alongside templated content.
- What Laravel versions or frameworks is this package compatible with?
- This package is **not** designed for Laravel—it’s built for **Yii2** (a separate PHP framework). If you’re using Laravel, use its built-in `Illuminate/Mail` or Symfony Mailer instead. For Yii2, it works with versions up to Yii2’s last major release (2023), but may require PHP 8.x compatibility fixes.
- How do I test email sending without actually sending emails?
- Enable file transport in your Yii2 config by setting `'useFileTransport' => true` in the mailer component. Emails will be saved to a directory (default: `runtime/mail/`), allowing you to verify content without sending. Disable this in production by setting it to `false`.
- Is this package actively maintained? What about SwiftMailer’s deprecation?
- The package has **no active maintenance** and relies on SwiftMailer, which is deprecated in favor of Symfony Mailer. If SwiftMailer’s deprecation causes issues, you’ll need to migrate to a modern alternative like Symfony Mailer or Laravel’s mail system. Consider this a legacy solution for Yii2 projects only.
- Can I use third-party email services like SendGrid or Mailgun with this package?
- Yes, but you’ll need to configure a custom transport. For example, use `Swift_SendmailTransport` for sendmail or create a custom transport class extending `Swift_Transport` to integrate with SendGrid/Mailgun APIs. The package supports any SwiftMailer-compatible transport, but modern API integrations may require additional coding.
- How do I handle email attachments or embedded images in Yii2 with this package?
- Attach files using the `attach()` method on the SwiftMailer message object (e.g., `$message->attach($filePath)`). For embedded images, use `embed()` and reference the CID in your HTML email. Example: `$message->embed(Yii::getAlias('@app/web/images/logo.png'))->setBody('<img src="cid:logo">', 'text/html')`.
- Will this package work with PHP 8.x, or do I need compatibility fixes?
- The last release (2018) likely targets PHP 7.1–7.3. For PHP 8.x, you may encounter issues with named arguments or type declarations. Check for community patches or consider forking the package to add PHP 8 support, though this is not officially maintained.
- What are the alternatives to yiisoft/yii2-swiftmailer for Yii2 or Laravel?
- For **Yii2**, no modern alternatives exist—this package is the de facto choice despite SwiftMailer’s deprecation. For **Laravel**, use `Illuminate/Mail` (built-in) or `symfony/mailer` (for Symfony Mailer integration). If migrating from Yii2 to Laravel, rewrite email logic using Laravel’s mail system or create a custom adapter layer.