CodeIgniter fail – Unable to send email using PHP mail()

I checked my google analytics and noticed some people went to my old blog (with hardly any articles). For the sake of history and information I will repost some blogs under the same URL 🙂

This is one of them (semi old, but hey.. I guess people still use codeigniter 😀 )

CodeIgniter fail

So, once again I was working on some fairly old project that uses the CodeIgniter framework. Now sure, there are a lot of valid points why you should not pick this framework anymore, but in my own opinion: it pretty much ‘works’ if your old or current project is based on it.

For my own projects I use a VPS with sometimes its own mail server. So far, so good. For the project I migrated the website to another host, with just a simple package. The package itself is fine, enough e-mail accounts, good speeds etc. There was only one downside.. no mail server. There ain’t a SMTP server that you can use.

Now you have two options, either use some 3rd party SMTP server (like gmail) or just use the plain PHP mail function. Since the hoster allowed this function, and the application is only sending a few e-mails every month, I used option #2: using the PHP mail function in CodeIgniter.

CodeIgniter actually has a decent e-mail class, in short its something like this:


$this->email->from('', 'Your Name');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');


You can add some config variables like the protocol. You can chose from mail, sendmail or smtp. So, obvious I picked mail, since I cannot use sendmail or smtp. You will end up with something like this:

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

Everything should work now, except for the fact I got this funky error.

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method. Now this is just some great error message from CI (CodeIgniter) saying he cannot send your mail.

For those who care, check system/libraries/Email.php on line 1490 for the function “_spool_email”, where it tries to e-mail your mail.

Now this error can actually be generated by like a million things, everything that ruins your e-mail and making sure it does not get send, will produce this error… Now the problem is to actually find the real problem 🙂 The best way is to check if you can actually send a e-mail with a simple PHP file, where you only have a line using mail($args). This way we do not use CI class, if the mail actually gets send we know the problem is within CI.

For me, the problem was tada in CI.

On line 1535 there is a function called “_send_with_mail”. Now I actually could not believe my eyes on the comment made in that function:

// most documentation of sendmail using the “-f” flag lacks a space after it, however
// we’ve encountered servers that seem to require it to be in place.

Well yes, thank and fuck you. There is this code line that actually sends the mail using the PHP mail function:

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

Now you can see the ‘-f’ parameter that actually includes that ‘spacing’ “because they saw servers having problems without the spacing”. This is very awesome, because they forgot: there are servers who are having problems with actually having that spacing. This was the whole problem on ‘my server’ rendering my entire mail function useless. So the simple fix is:

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

This behavior is weird and annoying, and we cannot blame CI directly for this but for someone who has not used the PHP mail function for a while, this is plain annoying. It took me a while to find out why I could not send any e-mails. Also because CI blames the server, rather than the code.

To be honest, when you are actually putting 2 comment lines in your function because you -know- there are problems with it, you should make a real fix. It is actually pretty easy to include some config var where you can say if you want a space or not. Obviously some servers need the spacing, other do not. A simple $config[‘parameter_spacing’] = true/false would have been enough!

Leave a Reply

Your email address will not be published. Required fields are marked *