'Your server might not be configured to send mail using this method

I want to verify email when users register. I am using codeigniter3.

When I run on the localhost I do not receive any mail. I get this message " Unable to send email using PHP mail(). Your server might not be configured to send mail using this method. "

Here is my code : controllers/User.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class User extends CI_Controller {
 
    function __construct(){
        parent::__construct();
        $this->load->model('users_model');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('session');
 
        //get all users
        $this->data['users'] = $this->users_model->getAllUsers();
    }
 
    public function index(){
        $this->load->view('register', $this->data);
    }
 
    public function register(){
        $this->form_validation->set_rules('email', 'Email', 'valid_email|required');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[7]|max_length[30]');
        $this->form_validation->set_rules('password_confirm', 'Confirm Password', 'required|matches[password]');
 
        if ($this->form_validation->run() == FALSE) { 
            $this->load->view('register', $this->data);
        }
        else{
            //get user inputs
            $email = $this->input->post('email');
            $password = $this->input->post('password');
 
            //generate simple random code
            $set = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $code = substr(str_shuffle($set), 0, 12);
 
            //insert user to users table and get id
            $user['email'] = $email;
            $user['password'] = $password;
            $user['code'] = $code;
            $user['active'] = false;
            $id = $this->users_model->insert($user);
 
            //set up email
            $config = array(
                'protocol' => 'mail',
                'smtp_host' => 'smtp.gmail.com',
                'smtp_port' => 587,
                'smtp_user' => '********@gmail.com', // change it to yours
                'smtp_pass' => '*********', // change it to yours
                'mailtype' => 'html',
                'wordwrap' => TRUE
            );
 
            $message =  "
                        <html>
                        <head>
                            <title>Verification Code</title>
                        </head>
                        <body>
                            <h2>Thank you for Registering.</h2>
                            <p>Your Account:</p>
                            <p>Email: ".$email."</p>
                            <p>Password: ".$password."</p>
                            <p>Please click the link below to activate your account.</p>
                            <h4><a href='".base_url()."user/activate/".$id."/".$code."'>Activate My Account</a></h4>
                        </body>
                        </html>
                        ";
 
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->from($config['smtp_user']);
            $this->email->to($email);
            $this->email->subject('Signup Verification Email');
            $this->email->message($message);
 
            //sending email
            if($this->email->send()){
                $this->session->set_flashdata('message','Activation code sent to email');
            }
            else{
                $this->session->set_flashdata('message', $this->email->print_debugger());
 
            }
 
            redirect('register');
        }
 
    }
 
    public function activate(){
        $id =  $this->uri->segment(3);
        $code = $this->uri->segment(4);
 
        //fetch user details
        $user = $this->users_model->getUser($id);
 
        //if code matches
        if($user['code'] == $code){
            //update user active status
            $data['active'] = true;
            $query = $this->users_model->activate($data, $id);
 
            if($query){
                $this->session->set_flashdata('message', 'User activated successfully');
            }
            else{
                $this->session->set_flashdata('message', 'Something went wrong in activating account');
            }
        }
        else{
            $this->session->set_flashdata('message', 'Cannot activate account. Code didnt match');
        }
 
        redirect('register');
 
    }
 
}



Solution 1:[1]

In your configuration change protocol to "smtp"

$config = array(
    'smtp_host'=> "smtp.gmail.com",
    "smtp_user"=> 'Username', // YOUR Username
    "smtp_pass" => 'Password', // YOUR Password
    'protocol' => 'smtp',
    'smtp_crypto' => 'ssl',
    "smtp_port" => 465,
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    'mailtype' => 'html',
    'newline'   => "\r\n"
);

If you are using gmail account, Make sure you have turn on "Less secure app" here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shamim Shaikh