Cannot instantiate abstract class Cake\Mailer\Mailer

I want to build forgot password page in cakephp
Here is my code of user controller
<?php

namespace App\Controller;

use App\Controller\AppController;
use Cake\Http\Exception\UnauthorizedException;

use Cake\Mailer\Email;
use Cake\Mailer\Mailer;
use Cake\email\TransportFactory;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Utility\Security;
use Cake\ORM\TableRegistry;
use Cake\Core\InstanceConfigTrait;

/**

  • Users Controller

  • @property \App\Model\Table\UsersTable $Users

  • @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
    */
    class UsersController extends AppController {

    public function beforeFilter(\Cake\Event\Event $event) {
    $this->Auth->allow([‘add’, ‘logout’]);
    parent::beforeFilter($event);
    }

    /**

    • Index method

    • @return \Cake\Http\Response|null
      */
      public function index() {
      if ($this->Auth->user(‘role’) != ‘admin’) {
      throw new UnauthorizedException(__(‘You are not allowed to access this page’));
      }
      $users = $this->paginate($this->Users);

      $this->set(compact(‘users’));
      }

    /**

    • View method

    • @param string|null $id User id.

    • @return \Cake\Http\Response|null

    • @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
      */
      public function view($id = null) {
      $user = $this->Users->get($id, [
      ‘contain’ => [],
      ]);

      $this->set(‘user’, $user);
      }

    /**

    • Add method

    • @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
      */
      public function add() {
      if ($this->Auth->user(‘role’) != ‘admin’) {
      throw new UnauthorizedException((‘You are not allowed to access this page’));
      }
      $user = $this->Users->newEntity();
      if ($this->request->is(‘post’)) {
      $user = $this->Users->patchEntity($user, $this->request->getData());
      if ($this->Users->save($user)) {
      $this->Flash->success(
      (‘The user has been saved.’));

           return $this->redirect(['action' => 'index']);
       }
       $this->Flash->error(__('The user could not be saved. Please, try again.'));
      

      }
      $this->set(compact(‘user’));
      }

    /**

    • Edit method

    • @param string|null $id User id.

    • @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.

    • @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
      */
      public function edit($id = null) {
      if ($this->Auth->user(‘role’) != ‘admin’) {
      throw new UnauthorizedException((‘You are not allowed to access this page’));
      }
      $user = $this->Users->get($id, [
      ‘contain’ => [],
      ]);
      if ($this->request->is([‘patch’, ‘post’, ‘put’])) {
      $user = $this->Users->patchEntity($user, $this->request->getData());
      if ($this->Users->save($user)) {
      $this->Flash->success(
      (‘The user has been saved.’));

           return $this->redirect(['action' => 'index']);
       }
       $this->Flash->error(__('The user could not be saved. Please, try again.'));
      

      }
      $this->set(compact(‘user’));
      }

    /**

    • Delete method

    • @param string|null $id User id.

    • @return \Cake\Http\Response|null Redirects to index.

    • @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
      */
      public function delete($id = null) {
      if ($this->Auth->user(‘role’) != ‘admin’) {
      throw new UnauthorizedException((‘You are not allowed to access this page’));
      }
      $this->request->allowMethod([‘post’, ‘delete’]);
      $user = $this->Users->get($id);
      if ($this->Users->delete($user)) {
      $this->Flash->success(
      (‘The user has been deleted.’));
      } else {
      $this->Flash->error(__(‘The user could not be deleted. Please, try again.’));
      }

      return $this->redirect([‘action’ => ‘index’]);
      }
      public function forgotpassword()
      {
      if ($this->request->is(‘post’)) {
      $email = $this->request->getData(‘email’);
      $token = Security::hash(Security::randomBytes(25));

      $userTable = TableRegistry::get(‘Users’);
      if ($email == NULL) {
      $this->Flash->error((‘Please insert your email address’));
      }
      if ($user = $userTable->find(‘all’)->where([‘email’=>$email])->first()) {
      $user->token = $token;
      if ($userTable->save($user)){
      $mailer = new Mailer(‘default’);
      $mailer->Transport(‘Smtp’);
      $mailer->From([‘noreply[at]codethepixel.com’ => ‘myCake4’])
      ->setTo($email)
      ->setEmailFormat(‘html’)
      ->setSubject(‘Forgot Password Request’)
      ->deliver(‘Hello
      Please click link below to reset your password

      Reset Password’);
      }
      $this->Flash->success(‘Reset password link has been sent to your email (’.$email.’), please check your email’);
      }
      if ($total = $userTable->find(‘all’)->where([‘email’=>$email])->count()==0) {
      $this->Flash->error(
      (‘Email is not registered in system’));
      }
      }
      }
      public function resetpassword($token)
      {
      if($this->request->is(‘post’)){
      $hasher = new DefaultPasswordHasher();
      $newPass = $hasher->hash($this->request->getData(‘password’));

      $userTable = TableRegistry::get(‘Users’);
      $user = $userTable->find(‘all’)->where([‘token’=>$token])->first();
      $user->password = $newPass;
      if ($userTable->save($user)) {
      $this->Flash->success(‘Password successfully reset. Please login using your new password’);
      return $this->redirect([‘action’=>‘login’]);
      }
      }
      }
      public function login() {
      if ($this->request->is(‘post’)) {
      $user = $this->Auth->identify();
      if ($user) {
      if ($user[‘is_active’] === 1) {
      $users = $this->Users->get($user[‘id’]);
      $users->ip_address = $this->request->clientIp();
      $users->last_login = date(‘Y-m-d h:i:s’);
      if ($this->Users->save($users)) {
      $this->Auth->setUser($user);
      return $this->redirect($this->Auth->redirectUrl());
      } else {
      $this->Flash->error((‘Unable to login by your credentials.’));
      }
      } else {
      $this->Flash->error(
      (‘This user not activated, please contact our administrator.’));
      }
      }
      $this->Flash->error(__(‘Invalid username or password, try again’));
      }
      }

    public function logout() {
    return $this->redirect($this->Auth->logout());
    }

}

Help me please.

You will in general get much better help if you include only relevant details. Not too much, but not too little. For example, here the “index” and “add” and “view” and “edit” and “delete” functions are probably unrelated to what you’re trying to do. Why include them?

Your code is also not all formatted well, for some reason, which makes it hard to read, and therefore harder to help.

Lastly, you say you want to “build forgot password page”, but you don’t say what your specific problem is. You have “forgotpassword” and “resetpassword” functions here. Do they not work? If not, what, specifically, fails? Or do you need help making a template? You haven’t shown what the templates for these pages look like, so we don’t know whether you just don’t have them at all, or if there’s some problem with how you’ve set up those forms, for example.

And really truly lastly, calling people out by name is, I think, a little rude. Help here is given voluntarily by people donating their time to help others. By naming people you are implicitly making a demand on them, which is not a good way to encourage the volunteer behaviour.

I am facing problem in building forgot password & reset password page this error occurs in forgot password page when user entered their registered email then click on submit then this error occurs " Connection refused".
I already provide my code if you can help me in building this then please help me.
add,login,index useful for my website thats why I need them.Currently I only need forgot password & reset password page.

I’m not disputing that those functions are useful for your site. But they don’t make one bit of difference to your problem, and having that massive block of code is likely preventing people from trying to help you. The key to getting good help is asking a good question. I’ve given you some tips on how to improve your question (note that you can edit it to improve it). You certainly choose not to listen…

SMTP timeout. error

connection refused error

So the error isn’t “Cannot instantiate abstract class”? It’s like you’re actively trying to make it harder to help you solve this problem instead of easier…

Yes I am working on this would you like to help me by suggesting some answer.
Its my battle with this since from last 46 days I search & try each every solutions on google related to cakephp on forgot password page but nothing works.

Is better to read the book here Mailer - 4.x for version 4.x or here Email - 3.10 for version 3.x and also explore config/app.php file to configure your ‘EmailTransport’ section properly.

Thanks for your reply I will tried them once again.

I tried them but none of them working.

I tried them but none of them working.

I tried your suggestion earlier none of them working.

Despite of giving answer to my problem you only giving me speech please I don’t need them. I need solution if you have then please told me otherwise don’t waste your or my time too.

Sorry you feel that way. I’m actually trying to help you to get a solution. Because the question is pretty much unanswerable as it stands. There are things you can do to improve the chances of that. If you don’t want to listen to that advice, that’s entirely your prerogative, but it shouldn’t be surprising then when a solution doesn’t appear.

As Zuluru says, the lack of relevant information makes this hard to diagnose.
So guessing some suggestions.
Assuming you can’t send any emails at all, and the transport is correctly set with proper port, security etc I would switch OFF your antivirus software temporarily and see if that solves the problem.
AVG is known to cause problems in this respect.

I suppose the problem is more related to you server setup then cakephp.
However, you wrote the error is “SMTP timeout”, so I’d suggest the following:

  • Check if you can send emails using that configuration directly from your server (e.g. using sendmail on linux systems)

if that works:

  • Check Email-configuration named “default” - you use it, but is it configured properly?

else:

  • It may be that (depending on your cloud-provider/ISP) communicating to email-servers on specific ports (like 25, 587) is blocked anyway. Check with your cloud-provider/ISP on how to proceed here.

Just some thoughts from a sysadmin.

Last but not least: “speech” is important on a platform like this. People like Zuluru take much time trying to help others but feeling like this isn’t honored from questioners by, for example, formatting code properly or being rude isn’t funny. You don’t want to do non-funny things either.