CodeIgniter unable to send email using PHP mail()

I'm trying to send an e-mail with Codeigniter like this:

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

$this->email->from("[email protected]");
$this->email->reply_to("[email protected]");
$this->email->to("[email protected]");
$this->email->subject("Test mail");
$this->email->message("Email body");
$this->email->set_alt_message("Email body txt");
$this->email->send();

and I got this on the email debugger: Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.

If I do e simple PHP mail() function with the same addresses, it works but when I use CodeIgniter it gives me the error. So why would it work with simple mail() but not with CodeIgniter ? Any ideas ?

Thanks.


Solution 1:

Had similar issue.

That's working code from the controller :

        $config = array();
        $config['useragent']           = "CodeIgniter";
        $config['mailpath']            = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
        $config['protocol']            = "smtp";
        $config['smtp_host']           = "localhost";
        $config['smtp_port']           = "25";
        $config['mailtype'] = 'html';
        $config['charset']  = 'utf-8';
        $config['newline']  = "\r\n";
        $config['wordwrap'] = TRUE;

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

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

        $this->email->from($fromEmail, $fromName);
        $this->email->to($email);

        $this->email->subject('Тест Email');
        $this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE));

        $this->email->send();

Solution 2:

Clearly, there does not seem to be a definitive 'one size fits all' answer. What worked for me was changing

$config['protocol'] = 'smtp';

TO:

$config['protocol'] = 'mail';

Hope this helps...

Solution 3:

Do you have an email.php file in your config folder? Maybe there's a problem with your configuration in there.