rmrevin/yii2-postman
Yii2的邮件模块。
2.4.0
2017-05-03 13:05 UTC
Requires
- phpmailer/phpmailer: ~v5.2
- yiisoft/yii2: ~2.0
Requires (Dev)
- phpunit/phpunit: ^5.2
README
安装
在 composer.json
{
"require": {
"rmrevin/yii2-postman": "~2.2"
}
}
配置
/config/web.php
<? return [ // ... 'components' => [ // ... 'postman' => [ 'class' => 'rmrevin\yii\postman\Component', 'driver' => 'smtp', 'default_from' => ['mailer@somehost.com', 'Mailer'], 'subject_prefix' => 'Sitename / ', 'subject_suffix' => null, 'table' => '{{%postman_letter}}', 'view_path' => '/email', 'smtp_config' => [ 'host' => 'smtp.domain.cpom', 'port' => 465, 'auth' => true, 'user' => 'email@domain.cpom', 'password' => 'password', 'secure' => 'ssl', 'debug' => false, ] ], ], // ... ];
如果你想要使用不加密的SMTP
<? return [ // ... 'components' => [ // ... 'postman' => [ 'class' => 'rmrevin\yii\postman\Component', // ... 'smtp_config' => [ // ... 'secure' => '', 'smtpAutoTls' => false, ] ], ], // ... ];
更新数据库模式
在你下载并配置了yii2-postman之后,你需要通过应用迁移来更新你的数据库模式
在 /config/console.php
<? return [ // ... 'components' => [ // ... 'postman' => [ 'class' => 'rmrevin\yii\postman\Component', ], ], // ... ];
在 命令行
php yii migrate/up --migrationPath=@vendor/rmrevin/yii2-postman/migrations/
使用方法
<? // ... (new \rmrevin\yii\postman\RawLetter()) ->setSubject('Subject') ->setBody('Message body') ->addAddress('user@somehost.com') ->addBccAddress(['tech@somehost.com']); if(!$Letter->send()){ echo $Letter->getLastError(); } // path to view algorithm: // Yii::$app->controller->module->getViewPath() . Postman::$view_path . '/' . 'message-view.php' // path to view: /protected/views/email/message-view.php (new \rmrevin\yii\postman\ViewLetter) ->setSubject('Subject') ->setBodyFromView('letter-view', [ 'name' => 'Rosy', 'date' => date('Y-m-d') ]) ->addAddress(['user@somehost.com', 'John Smith']) ->addAttachment('/path/to/file.tar.gz'); if(!$Letter->send()){ echo $Letter->getLastError(); }
定时任务
在定时任务脚本中
LetterModel::cron($num_letters_per_step = 10)