zazymko / yii-swiftmailer
Yii 框架的 SwiftMailer 集成
2.0.0
2018-01-04 13:38 UTC
Requires
- swiftmailer/swiftmailer: ~6.0
- yiisoft/yii: >=1.1.19
This package is not auto-updated.
Last update: 2024-09-21 01:34:48 UTC
README
此扩展为 SwiftMailer 6.0.x 提供了 Yii 框架 1 的邮件解决方案。
安装
运行以下命令:
composer require zazymko/yii-swiftmailer
或者将以下内容添加到你的 composer.json 文件的 require 部分:
"zazymko/yii-swiftmailer": "~2.0.0"
配置
邮件组件的配置取决于你选择的扩展。通常,你的应用程序配置应该看起来像这样:
return [ //.... 'components' => [ 'mailer' => [ 'class' => 'zazymko\swiftmailer\swift\Mailer', ], ], ];
示例 SMTP
return [ //.... 'components' => [ 'mailer' => [ 'class' => 'zazymko\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' => 'zazymko\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 \zazymko\swiftmailer\View */ /* @var $message \zazymko\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 \zazymko\swiftmailer\View */ /* @var $message \zazymko\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); ?>"> ```# yii-swiftmailer