CodeIgniter unable to send email using PHP mail()

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();

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...


Nobody seemed to really find a definitive answer, so I did some digging around and found out why.

in system/libraries/Email.php, first look at line 1552:

if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))

it seems to send everything all peachy like. I had the exact same symptoms too. To see if I was crazy, i inserted immediately before...

mail($this->_recipients, $this->_subject, $this->_finalbody)

so I basically removed all the headers and let PHP put in the defaults. Bingo! Without CI's headers, it works. With the CI headers, it doesn't. So what is it?

Digging around some more, I looked up to where html is initialized and used. Turns out it doesn't really do anything until around 1046, where it builds the message body.

from line 1048:

if ($this->send_multipart === FALSE)
{
    $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
    $hdr .= "Content-Transfer-Encoding: quoted-printable";
}
else
{
    $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;

    $body .= $this->_get_mime_message() . $this->newline . $this->newline;
    $body .= "--" . $this->_alt_boundary . $this->newline;

    $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
    $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
    $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;

    $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
    $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
}

Flipping send_multipart between TRUE and FALSE will make the mail class work or not work.

Looked through the Code Ignitor's email class docs reveals nothing. Going to line 52:

var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override.  Set to FALSE for Yahoo.

So there you have it. Possibly an error in how CI does multipart messages? The hidden config preference

$config['send_multipart'] = FALSE;

in the email.php seems to do the trick.


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