Codeigniter 3 应用程序错误:无法通过电子邮件发送有效的 link

Codeigniter 3 applcation bug: unable to send valid link via email message

我正在 blog application Codeigniter 3.1.8 和 Bootstrap 4.

我已经为这个应用程序添加了一个注册和登录系统。我目前正在开发密码重置系统。

我能够分别做这两件事

  1. 发送包含虚拟文本的密码重置电子邮件。
  2. 创建有效密码重置link。

但是,我无法在将重置 link 插入电子邮件 正文后发送电子邮件。

这是控制器:

class Newpassword extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
    }

    private $sender_email = "noreply@yourdomain.com";
    private $sender_name = "Razvan Zamfir";
    private $user_email = '';
    private $subject = 'Pasword reset link';
    private $reset_token = '';
    private $reset_url = '';
    private $reset_link = '';
    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()) {

                  //Get user email
                  $this->user_email = $this->input->post('email');

                   //create token
                   $this->reset_token = md5(str_shuffle($this->user_email));

                    //create url
                       $this->reset_url = base_url('changepasword/') . $this->user_email . '/'. $this->reset_token;

                //create reset link
                $this->reset_link = '<a href="' . $this->reset_url . '">password reset link</a>';

                echo $this->reset_link;die();

                $this->body = "Here is your $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->load->library('email', $config);
        } else {
          $this->email->initialize($config);
        }

        // Build the body and meta data of the email message
        $this->email->from($this->sender_email, $this->sender_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");
        }
    }
}

我已经检查了 link,它是有效的并且 href 属性的值符合预期,但是一旦我删除 echo $this->reset_link;die() 我就会看到发送电子邮件的尝试失败:

我的错误在哪里?

你运行是在本地吗?如果是这样,这可能是错误的原因。无法从本地计算机发送电子邮件。如果不能,请与您的托管公司联系,看看它是否允许您通过 php 发送邮件。大多数共享主机禁用此选项并出售 SMTP 服务以允许您发送电子邮件

您是否尝试过将视图加载到您的电子邮件功能中?然后您可以将内容传递给该文件并发送 html 电子邮件

电子邮件未发送问题可能有多种原因:

  1. Codeigniter 3 电子邮件库配置不正确

测试:使用 php 中的内置 email() 并测试它是否有效

  1. php 发送电子邮件的配置不正确
  2. 服务器未配置为移交电子邮件
  3. DNS 有外部 mx 记录,本地电子邮件未从服务器转发出去

test:使用外部 SMTP 服务器(免费的 gmail 很好)并像这样配置 ci3 库 https://forum.codeigniter.com/thread-75525-post-371979.html#pid371979

$config['protocol']  = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'your_email';
$config['smtp_pass'] = 'your_password';
$config['smtp_port'] = 465;
$config['charset']   = 'utf-8';
$config['mailtype']  = 'html';
$config['newline']   = "\r\n"; 

如果这不起作用,请使用

检查原因
if ($this->email->send(FALSE))
{
    echo $this->email->print_debugger();
}
  1. 服务器无法访问远程端口防火墙没有为此打开

测试: 关闭防火墙或selinux再测试

这对我有用:

public function sendResetMail()
    {
        // Email settings
        $config['protocol'] = 'sendmail';
        $config['smtp_host'] = 'mail.mydomain.com';
        $config['smtp_user'] = 'myemail@mydomain.com';
        $config['smtp_pass'] = '******';
        $config['smtp_port'] = 465;
        $config['charset']  = 'utf-8';
        $config['mailtype'] = 'html';
        $config['newline']   = "\r\n"; 
        
        if (!$this->load->is_loaded('email')) {
            $this->load->library('email', $config);
        } else {
            $this->email->initialize($config);
        }
        
        // Build the body and meta data of the email message
        $this->email->from($this->sender_email, $this->sender_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");
        }
}