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

问题描述 投票:0回答:2

我正在尝试将忘记密码功能实现到codeigniter但我面临一些像这样的问题,请帮我解决这个问题。此外,当我将协议更改为邮件时,它会给我另一个错误,

无法使用PHP mail()发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。

完整错误如下。

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" <[email protected]>
    Return-Path: <[email protected]>
    To: [email protected]
    Subject: =?UTF-8?Q?Reset=20your=20passwrod?=
    Reply-To: <[email protected]>
    User-Agent: CodeIgniter
    X-Sender: [email protected]
    X-Mailer: CodeIgniter
    X-Priority: 3 (Normal)
    Message-ID: <[email protected]>
    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--

我的forgot_password()代码部分如下。

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'=>'[email protected]',
                    '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('\r\n');
                $this->email->from('[email protected]','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("\r\n");
                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";
            }
        }

    }

我如何在localhost上的Codeigniter中发送电子邮件?

php codeigniter forgot-password
2个回答
0
投票

@ Arzoo Gajera

当电子邮件是实时服务器时,可以发送电子邮件。

但是,如果要在localhost中设置电子邮件工作设施,则必须按如下所示设置SMTP配置。

注意: - 这只是SMTP配置的基本设置。不同的电子邮件库可能具有不同的语

//Load email Library
$this->load->library('email');

$email_config ['charset']       = 'utf-8';
$email_config ['mailtype']      = 'html';
$email_config ['newline']       = "\r\n";

if($_SERVER['HTTP_HOST']=="localhost")
{
  //SMTP configuration
  $email_config = array(
                'smtp_host'=>'ssl://smtp.gmail.com';
                'smtp_user'=>'[email protected]',
                'smtp_pass'=>'*******',
                'smtp_port'=>465;
                'protocol' =>'smtp',
            );
}

因此,$email_config ['charset'] = 'utf-8';$email_config ['mailtype'] = 'html';$email_config ['newline'] = "\r\n";是localhost和Live-server中使用的COMMON电子邮件配置变量。

当它是localhost时,电子邮件功能将使用// SMTP配置$email_config数组。

注意: - 请记住,当您的网站进入LIVE服务器时,建议您从所有文件中删除此if($_SERVER['HTTP_HOST']=="localhost")电子邮件条件,无论您使用它。

编辑: - (重要)请更改以下行

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

因为\ n仅表示新行,如果它是双引号。在单引号中,它表示文字字符\ n。


0
投票

请先加载电子邮件库:

$这个 - >负载>库( '电子邮件');

© www.soinside.com 2019 - 2024. All rights reserved.