当用户忘记密码时,我想使用 CakePHP 电子邮件向具有新密码的用户发送电子邮件。如果用户忘记了密码,他们将被要求输入用户名和相应的电子邮件。系统将检查数据库中的用户名和电子邮件是否匹配,如果匹配,则创建一个随 secret 码并将其发送到用户的电子邮件帐户。我是初学者,我不确定我是否做得正确。我的代码如下:
员工 Controller :
<?php
App::uses('AppController', 'Controller');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
App::uses('CakeEmail', 'Network/Email');
class EmployeesController extends AppController {
//some code
public function forgotpassword()
{
$username=$this->request->data['fusername'];
//App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
//$passwordHasher = new SimplePasswordHasher();
//$password = $passwordHasher->hash($this->request->data['password']);
$emailaddress=$this->request->data['emailadd'];
$msg = $this->Employee->getUserPassword($username,$emailaddress);
if($msg)
{
foreach ($msg as $userdetails)
{
$userhashedpass=$userdetails['Employee']['employee_pw'];//see access level here
$userid=$userdetails['Employee']['id'];
$useremail=$userdetails['Employee']['employee_email'];
}
$newpassword="$userhashedpass[0]$userhashedpass[4]$userhashedpass[13]$userhashedpass[8]$userhashedpass[11]$userhashedpass[12]";
//send email
$Email = new CakeEmail();
$Email->from(array('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b9b6adb2b987be98b0b7acb5b9b1b4f6bbb7b5" rel="noreferrer noopener nofollow">[email protected]</a>' => 'CentreVision CRM'))
->to($useremail)
->subject('New Password')
->send('Your new password is $newpassword ');
/* $to = $useremail;
$subject = "Your New Password";
$txt = "Your new password is $newpassword ";
$headers = "From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b0a0f0602052b080e051f0e191d021802040545080406" rel="noreferrer noopener nofollow">[email protected]</a>" . "\r\n";
*/
//send email to the employee
// $reply=$this->Employee->updatenewpassword($username,$emailaddress,$newpassword);
$data = array('id' => $userid, 'employee_pw' => $newpassword);
// This will update Recipe with id 10
$this->Employee->save($data);
//$this->set('msg',$userhashedpass);
//$this->set('newpassword',$newpassword);
$errormsg="Email has been sent to registered email address linked to this username";
//comment out next line to not display the new password on the screen once smtp is configured
$this->set('newpassword',$newpassword);
$this->set('errorfor',$errormsg);
$this->render("../Pages/home");
$this->layout = '../Pages/home';
}
else{
$errormsg="Username and Email does not match";
$this->set('errorfor',$errormsg);
$this->render("../Pages/home");
$this->layout = '../Pages/home';
}
}
//some code
}
还有我的 app/config/email.php:
class EmailConfig {
public $default = array(
'transport' => 'Mail',
'from' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3e312a353e00391f37302b323e3633713c3032" rel="noreferrer noopener nofollow">[email protected]</a>',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('site@localhost' => 'My Site'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $fast = array(
'from' => 'you@localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'Smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => true,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
当我输入用户名和电子邮件时,出现以下错误:
mail():无法连接到“localhost”端口 25 的邮件服务器,请验证 php.ini 中的“SMTP”和“smtp_port”设置或使用 ini_set()
有人可以帮我吗?
最佳答案
只需使用一种配置:
$Email = new CakeEmail();
$Email->config('default');
或者根据您的情况:
$Email = new CakeEmail();
$Email->config('smtp');
或者如果您想使用 Gmail:
$Email = new CakeEmail();
$Email->config('gmail');
和配置:
class EmailConfig {
/*public $default = array(
'transport' => 'Mail',
'from' => 'you@localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('soyazucar@localhost' => 'Test Site'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);*/
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385f4d5d4b4b4b785f55595154165b5755" rel="noreferrer noopener nofollow">[email protected]</a>',
'password' => 'password',
'transport' => 'Smtp'
);
/*public $fast = array(
'from' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f0fcfde7e1f6e1f2e0bdf2fefafff8f2e1d3f4fef2faffbdf0fcfe" rel="noreferrer noopener nofollow">[email protected]</a>',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'Smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => true,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);*/
}
关于email - CakePHP 电子邮件配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23737451/