Resolving Error with Sending Emails via SMTP Using CodeIgniter

November 18th, 2014 - Posted by Steve Marks to PHP, Web Development.

When sending emails using the CodeIgniter Email Class, I’ve always just kept the protocol as ‘mail‘ meaning emails get sent using the standard PHP mail() function.

Recently however, on an application we had been developing, emails were a critical part of the system. We had to give them the best chance of being received successfully, and do our best to ensure they didn’t get rejected by spam filters. As a result we turned to sending emails via the SMTP protocol.

Fortunately, CodeIgniter’s Email Class can accept some additional configuration options to get this setup easily. This looks like something like so:

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

$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'xxx';
$config['smtp_user'] = 'xxx';
$config['smtp_pass'] = 'xxx';
$config['smtp_port'] = 25;
$this->email->initialize($config);

Oddly, when we’d performed the above the change and began sending emails we saw the following error from the debugger:

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

We tried to use different encoding, different subjects, recipients, senders, basically everything but we continued to get the error message above.

The Solution

After a fair bit of research we discovered that the issue was to do with line breaks and the way in which the SMTP host interprets them. The default new line character is \n, whereas the SMTP server is expecting \r\n. It never finds that so thinks that it never reached the end, ultimately causing a timeout.

Fortunately, changing this is simple. We can simply add the following after our call to the ‘initialize()‘ function:

$this->email->set_newline("\r\n");

Our final code then looks something like so:

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

$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'xxx';
$config['smtp_user'] = 'xxx';
$config['smtp_pass'] = 'xxx';
$config['smtp_port'] = 25;
$this->email->initialize($config);

$this->email->set_newline("\r\n");

// Send email(s) here...

I’m not sure if this is only the case with certain SMTP servers. It solved it for us however and hope it proves helpful if you’re encountering the same error.

This entry was posted on Tuesday, November 18th, 2014 at 7:46 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (11)
  1. Thanks for solution, worked for me

  2. alin says:

    thank u very much

  3. Robercick says:

    Thank you. It worked like charm.

  4. Works nicely here! thanks a lot man!

  5. Julio Graciel says:

    Thank you.

    It was the only way I could fix the problem.

  6. ABA says:

    Thanks you very very very muchhhh!!! (nothing about this in the ovh site)

  7. Claudio says:

    Thanks man! I need to say that you saved my life! I love you (in a good way)!

  8. Ed Wade says:

    THANK YOU THANK YOU THANK YOU!!!

    You absolute star!!!

    THANK YOU THANK YOU THANK YOU!!!

  9. Burim says:

    Thank you so much for this solution! I worked like 3 hours to find out what it was ^^

  10. Taha says:

    ENG : God bless you man :D
    TURKISH : Allah razı olsun hacı dayı :D

  11. ilmars says:

    Thanks for solution, worked for me