yiicod / yii2-mailqueue
为 Yii2 框架提供的简单强大的邮件队列
1.1.1
2018-02-18 21:00 UTC
Requires
- yiicod/yii2-base: 1.*
- yiicod/yii2-cron: 1.*
- yiisoft/yii2: 2.*
Requires (Dev)
README
邮件队列用于发送邮件。您无需考虑将发送多少封邮件,因为当您安装此扩展时,您可以设置此功能。您将有一个表,可以查看邮件的状态。
安装此扩展的首选方式是通过 composer。
运行
php composer.phar require --prefer-dist yiicod/yii2-mailqueue "*"
或者在您的 composer.json 的 require 部分添加
"yiicod/yii2-mailqueue": "*"
to the require section of your composer.json.
运行
php yii migrate/up --migrationPath=@vendor/yiicod/yii2-mailqueue/migrations
请注意,消息是用 Yii::t()
包装的,以支持消息翻译,如果您不使用 i18n,则应定义它们的默认消息源。
'i18n' => [ 'translations' => [ '*' => [ 'class' => 'yii\i18n\PhpMessageSource' ], ], ],
配置
使用 pm2(https://pm2.node.org.cn/) 作为守护进程管理器。
'components' => [ 'mailqueue' => [ 'class' => 'yiicod\mailqueue\WorkerCommand', ], ] ... 'bootstrap' => array('mailqueue')
完整的配置可以在 yiicod\mailqueue\config 中找到。
控制台命令
'commandMap' => [ 'mail-queue' => [ 'class' => 'yiicod\mailqueue\commands\MailQueueCommand', ], ],
或者使用 pm2(https://pm2.node.org.cn/)。这个变体更受欢迎。
'commandMap' => [ 'mail-queue' => [ 'class' => 'yiicod\mailqueue\commands\WorkerCommand', ], ],
pm2 配置
{ "apps": [ { "name": "job-queue", "script": "yii", "args": [ "mailqueue/work" ], "exec_interpreter": "php", "exec_mode": "fork_mode", "max_memory_restart": "1G", "watch": false, "merge_logs": true, "out_file": "runtime/logs/job_queue.log", "error_file": "runtime/logs/job_queue.log" } ] }
运行 PM2 守护进程
pm2 start daemons-app.json
迁移使用
迁移命令或使用手动(https://yiiframework.cn/doc-2.0/guide-db-migrations.html) 进行配置
yii migrate --migrationPath=@yiicod/mailqueue/migrations
推送到队列
/** * Add mail from queue * @param string $to Email to * @param string $subject Email subject * @param string $body email, html * @param string|Array $from From email * @param string $attachs Attach for email array('path' => 'file path', 'name' => 'file bname') * @param Array $additionalFields Any additional fields */ Yii::app()->mailQueue->push($to, $subject, $body, $from = '', array $attachs = [], $additionalFields = []);
或者
/** * Push mass * array( * array( * 'field name to' => '', * 'field name subject' => '', * 'field name body' => '', * 'field name priority' => '', * 'field name from' => '', * 'field name attachs' => '', * ) * ) * @param Array $data * @return int Return int */ Yii::app()->mailQueue->pushMass($data)