为什么在此 Codeigniter 3 应用程序中通过电子邮件发送密码重置 link 失败?
Why does sending a password reset link via email fail in this Codeigniter 3 application?
我正在 blog application Codeigniter 3.1.8 和 Bootstrap 4.
我已经为这个应用程序添加了一个注册和登录系统。
我在开发密码重置功能时 运行 遇到了这个问题:他重置 link 的电子邮件没有发送(或者可能没有收到)。
密码重置表单采用注册时使用的电子邮件地址:
<?php echo form_open(base_url('newpassword')); ?>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group mb-2">
<input type="submit" value="Reset password" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
控制器:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
private $headers = '';
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_link = '<a href="#">Dummy Reset Link</a>';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
$this->user_email = $this->input->post('email');
$this->body = "Your password reset link: $this->reset_link\n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
$this->headers = "From: noreply@yourdomain.com\n";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
if (mail($this->user_email, $this->subject, $this->body, $this->headers)) {
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
} else {
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}
检查邮箱是否存在的方法(注册时也用过,有效):
public function email_exists() {
$query = $this->db->get_where('authors', ['email' => $this->input->post('email')]);
return $query->num_rows() > 0;
}
虽然我收到确认电子邮件已发送的闪现消息,但实际上我并没有收到电子邮件。
我的赌注在哪里?
我修改了你的 class 以使用内置电子邮件库并混合了我的工作项目中的代码:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
// Sender email
private $my_email = "example@example.com";
// Sender name
private $my_name = "Example";
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_link = '<a href="#">Dummy Reset Link</a>';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
$this->user_email = $this->input->post('email');
$this->body = "Your password reset link: $this->reset_link\n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
// Loading the Email library
$config['protocol'] = 'sendmail';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
if($this->load->is_loaded('email')){
$this->email->initialize($config);
}
else{
$this->load->library('email',$config);
}
// Build the body and meta data of the email message
$this->email->from($this->my_email,$this->my_name);
$this->email->to($this->user_email);
$this->email->subject($this->subject);
$this->email->message($this->body);
if($this->email->send()){
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
}else{
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}
我正在 blog application Codeigniter 3.1.8 和 Bootstrap 4.
我已经为这个应用程序添加了一个注册和登录系统。
我在开发密码重置功能时 运行 遇到了这个问题:他重置 link 的电子邮件没有发送(或者可能没有收到)。
密码重置表单采用注册时使用的电子邮件地址:
<?php echo form_open(base_url('newpassword')); ?>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group mb-2">
<input type="submit" value="Reset password" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
控制器:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
private $headers = '';
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_link = '<a href="#">Dummy Reset Link</a>';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
$this->user_email = $this->input->post('email');
$this->body = "Your password reset link: $this->reset_link\n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
$this->headers = "From: noreply@yourdomain.com\n";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
if (mail($this->user_email, $this->subject, $this->body, $this->headers)) {
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
} else {
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}
检查邮箱是否存在的方法(注册时也用过,有效):
public function email_exists() {
$query = $this->db->get_where('authors', ['email' => $this->input->post('email')]);
return $query->num_rows() > 0;
}
虽然我收到确认电子邮件已发送的闪现消息,但实际上我并没有收到电子邮件。
我的赌注在哪里?
我修改了你的 class 以使用内置电子邮件库并混合了我的工作项目中的代码:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
// Sender email
private $my_email = "example@example.com";
// Sender name
private $my_name = "Example";
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_link = '<a href="#">Dummy Reset Link</a>';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
$this->user_email = $this->input->post('email');
$this->body = "Your password reset link: $this->reset_link\n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
// Loading the Email library
$config['protocol'] = 'sendmail';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
if($this->load->is_loaded('email')){
$this->email->initialize($config);
}
else{
$this->load->library('email',$config);
}
// Build the body and meta data of the email message
$this->email->from($this->my_email,$this->my_name);
$this->email->to($this->user_email);
$this->email->subject($this->subject);
$this->email->message($this->body);
if($this->email->send()){
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
}else{
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}