Codeigniter $this->email->send() not working while mail() does

I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)

But when run this Below code in my server it works for me.

application>controller>Sendingemail_Controller.php

public function send_mail() {
    $this->load->library('email');   
    $config = array();
    $config['protocol']     = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
    $config['smtp_host']    = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
    $config['smtp_user']    = "[email protected]"; // client email gmail id
    $config['smtp_pass']    = "******"; // client password
    $config['smtp_port']    =  465;
    $config['smtp_crypto']  = 'ssl';
    $config['smtp_timeout'] = "";
    $config['mailtype']     = "html";
    $config['charset']      = "iso-8859-1";
    $config['newline']      = "\r\n";
    $config['wordwrap']     = TRUE;
    $config['validate']     = FALSE;
    $this->load->library('email', $config); // intializing email library, whitch is defiend in system

    $this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break

    $from_email = $this->input->post('f_email'); // sender email, coming from my view page 
    $to_email = $this->input->post('email'); // reciever email, coming from my view page
    //Load email library

    $this->email->from($from_email);
    $this->email->to($to_email);
    $this->email->subject('Send Email Codeigniter'); 
    $this->email->message('The email send using codeigniter library');  // we can use html tag also beacause use $config['mailtype'] = 'HTML'
    //Send mail
    if($this->email->send()){
        $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
        echo "email_sent";
    }
    else{
        echo "email_not_sent";
        echo $this->email->print_debugger();  // If any error come, its run
    }
}

and my view page where I defined f_email and email comes through post method. application>view>emailtesting.php

<html>
<head>    
    <title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)"  required />
<input type = "email" name = "email"  placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>

if some error comes again please visit official documentation below:

https://codeigniter.com/user_guide/libraries/email.html


I faced this problem and found the following solution. Just a little change in the email config and it's working 100%:

$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';

Codeigniter User Guide: https://www.codeigniter.com/user_guide/libraries/email.html

This setup works for me:

$config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'Your SMTP Server',
            'smtp_port' => 25,
            'smtp_user' => 'Your SMTP User',
            'smtp_pass' => 'Your SMTP Pass',
            'mailtype'  => 'html'
            );
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);

$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address'); 

$this->email->subject('Email Subject');
$this->email->message('Email Message'); 

if($this->email->send()){
   //Success email Sent
   echo $this->email->print_debugger();
}else{
   //Email Failed To Send
   echo $this->email->print_debugger();
}

Use this setup email..

$this->load->library('email');

$config['protocol']    = 'smtp';
$config['smtp_host']    = 'ssl://smtp.gmail.com';
$config['smtp_port']    = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user']    = '[email protected]';
$config['smtp_pass']    = 'password';
$config['charset']    = 'utf-8';
$config['newline']    = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not      

$this->email->initialize($config);

$this->email->from('[email protected]', 'sender_name');
$this->email->to('[email protected]'); 
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();

echo $this->email->print_debugger();