程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Codeigniter 3 应用程序错误:无法通过电子邮件发送有效链接大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Codeigniter 3 应用程序错误:无法通过电子邮件发送有效链接?

开发过程中遇到Codeigniter 3 应用程序错误:无法通过电子邮件发送有效链接的问题如何解决?下面主要结合日常开发的经验,给出你关于Codeigniter 3 应用程序错误:无法通过电子邮件发送有效链接的解决方法建议,希望对你解决Codeigniter 3 应用程序错误:无法通过电子邮件发送有效链接有所启发或帮助;

我正在Codeigniter 3.1.8和bootstrap 4中开发一个基本的blog application

我已向此应用程序添加了注册和登录系统。我目前正在研究密码重置系统。

我能够单独地做这两件事:

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

但是,一旦将重置链接插入电子邮件正文,我就无法发送电子邮件

这是他的控制器:

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

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

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

我的错误在哪里?

解决方法

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

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

test:在 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并再次测试

,

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

,

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

,

这就是让我醒来的原因:

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

大佬总结

以上是大佬教程为你收集整理的Codeigniter 3 应用程序错误:无法通过电子邮件发送有效链接全部内容,希望文章能够帮你解决Codeigniter 3 应用程序错误:无法通过电子邮件发送有效链接所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:3