【发布时间】:2021-02-20 19:34:59
【问题描述】:
我正在 Codeigniter 3.1.8 和 Bootstrap 4 中开发基本的 blog application。
我已经为这个应用程序添加了一个注册和登录系统。
我在开发密码重置功能时遇到了这个问题:包含重置链接的电子邮件未发送(或可能未收到)。
密码重置表格采用注册时使用的电子邮件地址:
<?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;
}
虽然我收到确认电子邮件已发送的 Flash 消息,但我实际上并没有收到电子邮件。
我的赌注在哪里?
【问题讨论】:
-
如果您使用 Codeigniter 电子邮件类 codeigniter.com/userguide3/libraries/email.html,您将能够对其进行调试。检查页面底部的“print_debugger”方法。
-
来自php docs:“重要的是要注意,仅仅因为邮件被接受投递,并不意味着邮件实际上会到达预定目的地。”
标签: php codeigniter codeigniter-3