doudou0111 2019-02-14 10:10 采纳率: 0%
浏览 134
已采纳

在localhost上的Codeigniter中实现忘记密码

I am trying to implement forgot password functionality into codeigniter but I face some issue like this someone, please help me to solve this. Also when I change the protocol to mail than it's give me one another error that,

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.

Complete error is as below.

A PHP Error was encountered

    Severity: Warning

    Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:25 (Connection timed out)

    Filename: libraries/Email.php

    Line Number: 2069

    The following SMTP error was encountered: 110 Connection timed out
    Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

    Date: Thu, 14 Feb 2019 10:52:06 +0100
    From: "arazu" <arazugajera98@gmail.com>
    Return-Path: <arazugajera98@gmail.com>
    To: arazugajera98@gmail.com
    Subject: =?UTF-8?Q?Reset=20your=20passwrod?=
    Reply-To: <arazugajera98@gmail.com>
    User-Agent: CodeIgniter
    X-Sender: arazugajera98@gmail.com
    X-Mailer: CodeIgniter
    X-Priority: 3 (Normal)
    Message-ID: <5c653a46009bc@gmail.com>
    Mime-Version: 1.0


    Content-Type: multipart/alternative; boundary="B_ALT_5c653a46009c6"

    This is a multi-part message in MIME format.
    Your email application may not support this format.

    --B_ALT_5c653a46009c6
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit

    Click Here if you want to reset your password


    --B_ALT_5c653a46009c6
    Content-Type: text/html; charset=UTF-8
    Content-Transfer-Encoding: quoted-printable

    =3Ca href=3D'http://127.0.0.1/CodeIgniter/login/reset=5Fpassword/a5ea8860b0=
    5acc175d4c1847fab1f2ee'=3EClick Here=3C/a=3E if you want to reset your pass=
    word

    --B_ALT_5c653a46009c6--

My forgot_password() code part is as follows.

public function forgot_password(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email','email','trim|required|valid_email');
        if($this->form_validation->run() == FALSE){
            echo "please enter email";
            $this->load->view('recover_password');
        }
        else{
            $email = $this->input->post('email');
            $result = $this->registration_model->email_exists($email);
            if($result == TRUE){
                $temp_pass=md5(uniqid());
                $email_config = array(
                    'smtp_host'=>'localhost',
                      'smtp_user'=>'arazugajera98@gmail.com',
                    'smtp_pass'=>'*******',
                    'smtp_port'=>25,
                    'charset'   => 'utf-8',
                    'protocol'=>'smtp',
                    'mailtype'=>'html'
                );
                $this->load->library('email');
                $this->email->initialize($email_config);

                $this->email->set_newline('
');
                $this->email->from('arazugajera98@gmail.com','arazu');
                $this->email->to($this->input->post('email'));
                $this->email->subject('Reset your passwrod');

                $message = "this email is sent you to reset your password";
                $message = "<a href='".base_url()."login/reset_password/$temp_pass'>Click Here</a> if you want to reset your password";

                $this->email->message($message);
                $this->email->set_newline("
");
                if($this->email->send()){
                    echo "hello"; exit();
                    if($this->registration_model->temp_reset_password($temp_pass)){
                        echo 'check your for instruction, thank you';
                    }
                }
                else{
                    //echo "email is not sent, please contact your administrator";
                     echo $this->email->print_debugger();
                }
            }else{
                echo "your email is not exists into database";
            }
        }

    }

How I send email in Codeigniter on localhost?

  • 写回答

2条回答 默认 最新

  • douzongmu2543 2019-02-14 18:18
    关注

    @Arazu Gajera

    Emails can be sent when it's the live server.

    BUT if you want to set email working facility in localhost, then you have to set SMTP configuration as below.

    NOTE:- This is just basic setup for SMTP configuration. Different Email libraries may have different syntax.

    //Load email Library
    $this->load->library('email');
    
    $email_config ['charset']       = 'utf-8';
    $email_config ['mailtype']      = 'html';
    $email_config ['newline']       = "
    ";
    
    if($_SERVER['HTTP_HOST']=="localhost")
    {
      //SMTP configuration
      $email_config = array(
                    'smtp_host'=>'ssl://smtp.gmail.com';
                    'smtp_user'=>'arazugajera98@gmail.com',
                    'smtp_pass'=>'*******',
                    'smtp_port'=>465;
                    'protocol' =>'smtp',
                );
    }
    

    so, $email_config ['charset'] = 'utf-8';, $email_config ['mailtype'] = 'html';, $email_config ['newline'] = " "; are COMMON email config variables used in both localhost and Live-server.

    And when it's localhost, email functionality will use //SMTP configuration $email_config array.

    NOTE :- Please keep in mind that when your website is going on LIVE Server, It's advisable to remove this if($_SERVER['HTTP_HOST']=="localhost") email condition from all files wherever you have used it.

    EDIT :- (IMPORTANT) Please change below line

    //$this->email->set_newline('
    ');
    $this->email->set_newline("
    ");  //Use double quotes for 
    
    

    Because only means new line if it is in double quotes. In single quotes it means the literal characters .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码