0

I'm working in CakePHP 3.4

I have setup Mailer to send verification email to user after registration.

/src/Mailer/UserMailer.php

<?php
namespace App\Mailer;

use Cake\Mailer\Mailer;

class UserMailer extends Mailer
{
    public function verify($user)
    {
        $this
            ->setProfile('no-reply')
            ->setTemplate('register')
            ->setLayout('authentication')
            ->setEmailFormat('html')
            ->setTo($user->email)
            ->setSubject('Verify Account')
            ->setViewVars(['name' => $user->first_name, 'email' => $user->email, 'hash' => $user->verification_hash]);
    }

    public function implementedEvents()
    {
        return [
            'Model.afterSave' => 'onRegistration'
        ];
    }

    public function onRegistration(Event $event, EntityInterface $entity, ArrayObject $options)
    {
        if ($entity->isNew()) {
            $this->send('verify', [$entity]);
        }
    }
}

But, it is not sending any email.

Emails are being sent when triggered manually from controller using

$this->getMailer('User')->send('verify', [$user]);
4

1 回答 1

0

请检查该onRegistration方法是否甚至被解雇。我认为你必须实施Cake\Event\EventListenerInterface.

请参阅文档

use Cake\Event\EventListenerInterface;

class UserMailer extends Mailer implements EventListenerInterface
{
    ...
}

编辑:好的,忽略我——刚刚发现Mailer班级已经在监听事件。

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

于 2017-05-02T13:34:34.530 回答