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]);
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

1 Answers1

0

Please inspect if the onRegistration method is even getting fired. I think you have to implement the Cake\Event\EventListenerInterface.

See the docs.

use Cake\Event\EventListenerInterface;

class UserMailer extends Mailer implements EventListenerInterface
{
    ...
}

Edit: Ok, ignore me – just found out the Mailer class already listens for events.

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

Marijan
  • 1,825
  • 1
  • 13
  • 18