问题描述
根据,我应该可以直接将实施的事件添加到我的邮件程序中,从我的代码中分离我的所有邮件逻辑。
According to documentation , I should be able to add implementedEvents directly to my mailer to seperate all my mailing logic from my codes.
但是,当我遵循文档中的确切示例时;我看到我实现的事件功能不起作用(不发送电子邮件,不记录任何内容)
However, when I follow the exact examples in documentation; I see my implemented event function does not work. (not sending emails & does not log anything)
我应该将邮箱类实施到某个地方吗?如果是,我该怎么注册我的电子邮件类?
Should I implement my emailer class to somewhere? If so, how should I register my emailer class?
这是我的邮件类:
<?php
namespace App\Mailer;
use Cake\Mailer\Mailer;
use Cake\Log\Log;
/**
* Purchase mailer.
*/
class PurchaseMailer extends Mailer
{
/**
* Mailer's name.
*
* @var string
*/
static public $name = 'Purchase';
public function implementedEvents()
{
return [
'Model.afterSave' => 'onStatusChange'
];
}
public function onStatusChange(Event $event, EntityInterface $entity, ArrayObject $options)
{
Log::write(
'info',
'd1'
);
//if ($entity->isNew()) {
$this->send('sendStatusChangeMails', [$entity]);
//}
}
/**
* @param EntityInterface $entity
* @return [type]
*/
public function sendStatusChangeMails($entity)
{
Log::write(
'info',
'd2'
);
//if($entity->status_id == 1) {
//@todo email???
$this
->template('purchase')
->layout('default')
->emailFormat('html')
->from(['[email protected]' => 'TEST'])
->to('[email protected]')
->subject('test')
->set(['content' => 'this is a purhcase testing mail.']);
//}
}
}
推荐答案
答案是Mailer类和事件文档。
Answer is the Mailer Class and Events docs.
所以你可以订阅在您的UsersTable初始化邮件中:
So you could subscribe in your mail in your UsersTable initialize:
public function initialize(array $config)
{
parent::initialize($config);
$mailer = new UserMailer(); //use App\Mailer\UserMailer;
$this->eventManager()->on($mailer);
//more code...
}
这篇关于CakePHP 3 implementationEvents()在电子邮件中没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!