Home > Software engineering >  How can i send an email in php with SMTP?
How can i send an email in php with SMTP?

Time:12-07

Hello please someone help me why am i having a hard time and i can't identify the error why does it returns error in set flash data although the email id match the output? i'm doing a forgot password in my login page using PHP and a codeigniter framework

do i need to install SMTP to enable the email method or do i need to install something?

controller:

public function index()
    {
        $this->load->model('model_users');
        if($_SERVER['REQUEST_METHOD']=='POST')
        {
            $this->form_validation->set_rules('email','Email','required');
            if($this->form_validation->run()==TRUE)
            {
                $email = $this->input->post('email');
                $validationemail = $this->model_forgotpass->validationemail($email);
                if($validationemail!=false)
                {
                    $row = $validationemail;
                    $user_id = $this->session->userdata('id');
                    $user_data = $this->model_users->getUserData($user_id);

                    $string = time().$user_id.$email;
                    $hash_string = hash('sha256',$string);
                    $currentDate = date('Y-m-d H:i');
                    $hash_expiry = date('Y-m-d H:i',strtotime($currentDate. ' 1 days'));
                    $data = array(
                        'hash_key'=>$hash_string,
                        'hash_expiry'=>$hash_expiry,
                    );
                    $this->model_forgotpass->updatePasswordhash($data,$email);

                    $resetLink = base_url().'reset/password?hash='.$hash_string;
                    $message = '<p>Your reset password Link is here:</p>'.$resetLink;
                    $subject = "Password Reset link";
                    $sentstatus = $this->sendEmail($email,$subject,$message);
                    if($sentstatus==true)
                    {
                        $this->model_forgotpass->updatePasswordhash($data,$email);
                        $this->session->set_flashdata('success','Reset password link successfully sent');
                        redirect(base_url('forgotpass/index'));
                    }
                    else
                    {
                        $this->session->set_flashdata('error','Email sending error');
                    }

                }
                else
                {
                    $this->session->set_flashdata('error','Invalid email id');
                }
            }
        }
        else
        {
            $this->load->view('forgotpass/index','refresh');
        }


        $this->load->view('forgotpass/index','refresh');
        
    }
    public function sendEmail($email,$subject,$message)
    {
        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.gmail.com',

            'smtp_port' => 465,
            'smtp_user' => 'xxx',
            'smtp_pass' => 'xxx',

            'mailtype' => 'html',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
        );
        $this->load->library('email',$config);
        $this->email->set_newline("\r\n");
        $this->email->from('noreply');
        $this->email->to($email);
        $this->email->subject($subject);
        $this->email->message($message);

        if($this->email->send())
         {
            return true;
         }
         else
         {
            return false;
         }
    }

this is my model:

function validationemail($email)
    {
        if($email) {
            $sql = 'SELECT * FROM users WHERE email = ?';
            $query = $this->db->query($sql, array($email));
            $result = $query->num_rows();
            return ($result == 1) ? true : false;
        }

        return false;
        
    }
    function updatePasswordhash($data,$email)
    {
        $this->db->where('email',$email);
        $this->db->update('users',$data);
    }

i can't seem to identify the error here it always return as a Email sending error and i still can't received the mssage in my email although my username and password is correct but why i can't process it?

CodePudding user response:

The easiest way to send mail using PHP is to use PHPMailer. I recommend you research this. https://github.com/PHPMailer/PHPMailer

CodePudding user response:

print error message using echo $this->email->print_debugger(); in email else part.

  • Related