简体   繁体   English

Codeigniter 3 应用程序错误:无法通过 email 消息发送有效链接

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

I am working on a basic blog application in Codeigniter 3.1.8 and Bootstrap 4.我正在Codeigniter 3.1.8和 Bootstrap 4 中开发一个基本的博客应用程序

I have added a registration and login system to this application.我在这个应用程序中添加了一个注册和登录系统。 I am current working on a password reset system.我目前正在研究密码重置系统。

I was able to do these 2 things separately :我能够分别做这两件事:

  1. Send a password reset email containing dummy text.发送包含虚拟文本的密码重置 email。
  2. Create a valid password reset link.创建一个有效的密码重置链接。

I was unable however, to send the email once the reset link was inserted into the email body.但是,一旦将重置链接插入 email 主体,我就无法发送 email

Here is he controller:这是他的controller:

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");
        }
    }
}

I have inspected the link, it is valid and the value of the href attribute is as intended, but once I remove echo $this->reset_link;die() I see the attempt to send the email failing:我检查了链接,它是有效的并且href属性的值符合预期,但是一旦我删除echo $this->reset_link;die()我看到尝试发送 email 失败:

在此处输入图像描述

Where is my mistake?我的错误在哪里?

The email not sending issue can be from multiple reasons: email 未发送问题可能有多种原因:

  1. Codeigniter 3 email library is not configured correctly Codeigniter 3 email 库配置不正确

test : use built in email() in php and test if that's working测试:使用 php 中的内置 email() 并测试它是否有效

  1. php not configured correctly to send email php 未正确配置以发送 email
  2. server is not configured for handing over emails服务器未配置为移交电子邮件
  3. DNS has external mx records and local email is not forwarded out from the server DNS 有外部 mx 记录,本地 email 没有从服务器转发出去

test : use external SMTP server (free gmail is fine) and configure like this the ci3 library https://forum.codeigniter.com/thread-75525-post-371979.html#pid371979 test : use external SMTP server (free gmail is fine) and configure like this the ci3 library 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 is not working, check what's the cause by using如果这不起作用,请使用以下方法检查原因

if ($this->email->send(FALSE))
{
    echo $this->email->print_debugger();
}
  1. server cannot access remote ports firewalls are not opened for this服务器无法访问远程端口防火墙未为此打开

test : disable firewall or selinux and test again测试:禁用防火墙或 selinux 并再次测试

Are you running it locally?你是在本地运行吗? If so this might be the cause of the error.如果是这样,这可能是错误的原因。 Can't send emails from local machines.无法从本地计算机发送电子邮件。 If not check with your hosting company if it allows you to send mails via php.如果不检查您的托管公司是否允许您通过 php 发送邮件。 Most shared hostings disable this option and sell SMTP service to allow you to send emails大多数共享主机禁用此选项并出售 SMTP 服务以允许您发送电子邮件

Have you tried loading a view into your email function?您是否尝试将视图加载到 email function 中? Then you could pass the content to that file and send a html email然后您可以将内容传递给该文件并发送 html email

This is what woked for me:这就是唤醒我的原因:

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");
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM