Create Helper file

function send_email($from, $subject, $message){

	$config = array('protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => '465 ', 'smtp_user' => 'youremail@gmail.com',
        'smtp_pass' => '*****', 'mailtype' => 'html');

        $CI = &get_instance();

        $CI->load->library('email');
        $CI->email->initialize($config);
        $CI->email->set_newline("\r\n");
        $CI->email->from('youremail@gmail.com', 'from_name');
        $CI->email->to('youremail@gmail.com');
        $CI->email->subject('Test');
        $CI->email->message('Hello test email is here');

        if ($CI->email->send())
        {
            
            $CI->session->set_flashdata('notice', 'Your Message sent successfully!');

        }
        else
        {            
            //echo $CI->email->print_debugger(); exit; //If you want to track when fire an error
            $CI->session->set_flashdata('notice', 'Your Message not sent try again');

        }

}

Some Common Errors while you sent an email from localhost

  • When it shows an error with authentication go to – https://accounts.google.com/DisplayUnlockCaptcha
  • Turn on less secure app – https://myaccount.google.com/lesssecureapps