CakePHP 3 implementationEvents()在电子邮件中没有触发 [英] CakePHP 3 implementedEvents() does not fire in emailer

查看:257
本文介绍了CakePHP 3 implementationEvents()在电子邮件中没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,我应该可以直接将实施的事件添加到我的邮件程序中,从我的代码中分离我的所有邮件逻辑。

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(['info@example.com' => 'TEST'])
                    ->to('test@test.com')
                    ->subject('test')
                    ->set(['content' => 'this is a purhcase testing mail.']);
        //}
    }


}


推荐答案

答案是Mailer类和事件文档。

Answer is the Mailer Class and Events docs.

https://api.cakephp.org/3.2/class-Cake.Mailer.Mailer.html

https://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners


我们的邮件程序可以在应用程序引导中注册,也可以在Table类的initialize()钩子中注册。

Our mailer could either be registered in the application bootstrap, or in the Table class' initialize() hook.

所以你可以订阅在您的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()在电子邮件中没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆