bashkarev / yii-swiftmailer
为 Yii 框架提供的 SwiftMailer 集成
1.0.0
2016-07-14 20:58 UTC
Requires
- swiftmailer/swiftmailer: ~5.0
- yiisoft/yii: >=1.1.14
This package is not auto-updated.
Last update: 2024-09-14 19:14:46 UTC
README
此扩展为 SwiftMailer 邮件解决方案提供 Yii 框架 1。
安装
运行以下命令
composer require bashkarev/yii-swiftmailer
或者
"bashkarev/yii-swiftmailer": "~1.0.0"
将以下内容添加到你的 composer.json 文件中的 require 部分。
配置
邮件组件配置取决于你选择的扩展。通常,你的应用程序配置应如下所示
return [ //.... 'components' => [ 'mailer' => [ 'class' => 'bashkarev\swiftmailer\swift\Mailer', ], ], ];
示例 SMTP
return [ //.... 'components' => [ 'mailer' => [ 'class' => 'bashkarev\swiftmailer\swift\Mailer', //'viewPath' => 'application.mail' //default path to views 'transport' => [ 'host' => 'smtp.example.ru.', 'username' => 'username', 'password' => 'password', 'port' => '465', 'encryption' => 'ssl', ], 'messageConfig' => [ 'from' => ['example@example.ru' => 'Example Name'] ] ], ], ];
调试面板
return [ //.... 'components' => [ 'debug' => [ 'class' => 'application.vendor.zhuravljov.yii2-debug.Yii2Debug', 'enabled' => YII_DEBUG, 'panels' =>[ 'mail' => [ 'class' => 'bashkarev\swiftmailer\debug\MailPanel' ] ], ], ], ];
基本用法
一旦配置了 'mailer' 组件,你可以使用以下代码来发送电子邮件消息
Yii::app()->mailer->compose() ->setFrom('from@domain.com') ->setTo('to@domain.com') ->setSubject('Message subject') ->setTextBody('Plain text content') ->setHtmlBody('<b>HTML content</b>') ->send();
你也可以一次发送多个消息
$messages = []; foreach ($users as $user) { $messages[] = Yii::app()->mailer->compose() // ... ->setTo($user->email); } Yii::$app->mailer->sendMultiple($messages);
编写邮件内容
Yii 允许通过特殊的视图文件来编写实际的邮件消息内容。默认情况下,这些文件应位于 'application.mail' 路径。
示例邮件视图文件布局:/protected/mail/layouts/html.php
<?php /* @var $this \bashkarev\swiftmailer\View */ /* @var $message \bashkarev\swiftmailer\MessageInterface */ /* @var $content string main view render result */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::app()->charset ?>" /> <style type="text/css"> .heading {...} .list {...} .footer {...} </style> </head> <body> <?= $content ?> <div class="footer">With kind regards, <?= Yii::app()->name ?> team</div> </body> </html>
示例邮件视图文件内容
/protected/mail/test.php
<?php /* @var $this \bashkarev\swiftmailer\View */ /* @var $message \bashkarev\swiftmailer\MessageInterface */ ?> <div class="mail-test"> User id:<?= $model->id ?> </div>
Yii::app()->mailer->compose('test', ['model'=>User::model()->findByPk(1)]) ->setFrom('from@domain.com') ->setTo('to@domain.com') ->setSubject('Message subject') ->send();
文件附件
你可以使用 attach()
和 attachContent()
方法向消息添加附件
$message = Yii::app()->mailer->compose(); // Attach file from local file system: $message->attach('/path/to/source/file.pdf'); // Create attachment on-the-fly $message->attachContent('Attachment content', ['fileName' => 'attach.txt', 'contentType' => 'text/plain']);
嵌入图像
你可以使用 embed()
方法将图像嵌入到消息内容中。此方法返回附件 ID,然后应在 'img' 标签中使用。当通过视图文件编写消息内容时,此方法易于使用
Yii::app()->mailer->compose('embed-email', ['imageFileName' => '/path/to/image.jpg']) // ... ->send();
然后在视图文件中,你可以使用以下代码
<img src="<?= $message->embed($imageFileName); ?>">