cakephp 无法示例化抽象类Cake\Mailer\Mailer

oxalkeyp  于 2022-11-12  发布在  PHP
关注(0)|答案(1)|浏览(154)

我想在cakephp中建立忘记密码页面
这是我的用户控制器代码

<?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());
}

}

打开忘记密码页面后,用户在收到错误后输入其电子邮件。
无法示例化抽象类Cake\Mailer\Mailer
我如何解决这个问题,当用户输入他们的电子邮件和重置密码将发送到用户的电子邮件,这将保存在我们的数据库。
请帮帮我@ndm

ui7jx7zq

ui7jx7zq1#

在您的app.php中

use Cake\Mailer\Transport\MailTransport;
.
.
.

'EmailTransport' => [
        'default' => [
            'className' => MailTransport::class,
            /*
             * The following keys are used in SMTP transports:
             */
            'host' => 'localhost',
            'port' => 25,
            'timeout' => 30,
            'username' => null,
            'password' => null,
            'client' => null,
            'tls' => null,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],

        'your_project' => [
            'className' => 'Smtp',
            'host' => 'your_host',
            'port' => XXX,
            'timeout' => 30,
            'username' => 'your_email',
            'password' => 'your_password',
            'client' => null,
            'tls' => true,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
    ],

    'Email' => [
        'default' => [
            'transport' => 'default',
            'from' => 'you@localhost',
            //'charset' => 'utf-8',
            //'headerCharset' => 'utf-8',
        ],
        'your_project' => [
            'transport' => 'your_project',
            'from' => ['your_email@teste.com' => 'My Site'],
            //'charset' => 'utf-8',
            //'headerCharset' => 'utf-8',
        ],
    ],

在src/Mailer中创建一个文件夹“Mailer”。创建一个UserMailer.php文件

<?php
namespace App\Mailer;

use Cake\Mailer\Mailer;

class UserMailer extends Mailer
{
    public static $name = 'User';

    public function reset($user)
    {
        $this->to($user->email)
        ->profile('your_project')
        ->emailFormat('html')
        ->template('your_project', 'user')
        ->layout('user')
        ->viewVars(['name' => $user->name])
        ->subject(sprintf('Welcome...',  $user->name))
        ->message("texte...");
    }
}

在这个例子中,我为我的电子邮件创建了一个布局。在布局中创建一个“电子邮件”文件夹。
在控制器上,执行以下操作:

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

不要忘记导入到控制器中:

use Cake\Mailer\MailerAwareTrait;

我是这样用的。
转到godaddy Jmeter 板并查找SMTP信息。这就是您所需要的全部信息!替换为您的信息

相关问题