我正在使用 Yii 2 和 yii2-queue 包从队列进程发送一批电子邮件。
class SendEmailReminder extends yiibaseBaseObject implements yiiqueueJobInterface
{
public $userId;
public function execute($queue)
{
$user = appmodelsUser::find()
->select(['id', 'email', 'username'])
->where('id=:uid AND verified=false')
->params([':uid' => $this->userId])
->asArray()
->one();
if ($user) {
$body = 'test mail';
Yii::$app->mailer->compose()
->setFrom(Yii::$app->params['fromEmail'])
->setTo('myemail@address')
->setSubject('Reminder')
->setTextBody($body)
->send();
}
}
}
在另一个控制器操作中,我有这个:
public function actionRemindUsersWithNoValidatedEmail()
{
$users = User::find()
->select(['id'])
->where(['and', 'verified=false', '(NOW() - registered) > INTERVAL '6 day''])
->asArray()
->all();
foreach ($users as $user) {
Yii::$app->queue->push(new SendEmailReminder([
'userId' => $user['id']
]));
}
}
我的网络.php配置:
'components' => [
..............
'queue' => [
'class' => yiiqueuefileQueue::class,
'path' => '@runtime/queues',
],
'mailer' => [
'class' => 'yiiswiftmailerMailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'host',
'username' => 'user',
'password' => 'pass',
'port' => '587',
'encryption' => 'tls',
]
],
]
和我的控制台.php配置:
'components' => [
'queue' => [
'class' => yiiqueuefileQueue::class,
'path' => '@runtime/queues',
],
'mailer' => [
'class' => 'yiiswiftmailerMailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'host',
'username' => 'user',
'password' => 'pass',
'port' => '587',
'encryption' => 'tls',
]
],
],
我调用操作提醒用户与未验证的电子邮件,我可以看到@runtime/队列填满了队列文件。如果我执行./yii queue/run
,我可以看到队列被清空(因此,处理(,但不幸的是没有发送电子邮件。我在这里错过了什么?
知道了。因为我从命令行运行./yii queue/run
,所以很难找到基本 URL。所以我不得不在控制台中插入这段代码.php(在组件部分内(:
'urlManager' => [
'class' => 'yiiwebUrlManager',
'scriptUrl' => 'http://myurl'
]
使用详细标志运行./yii queue/run -v
后,我发现了错误。谢谢大家!