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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

When you find that CodeIgniter's $this->load->library('email') fails to dispatch a message while a raw PHP mail() call succeeds, the issue usually isn't logic—it's configuration. The framework's Email library acts as an abstraction layer that attempts to construct a properly formatted SMTP or mail transport object based on your server environment. If those defaults don't match your infrastructure, the library throws exceptions or silently fails, whereas mail() simply grabs whatever PHP sees in its environment variables and fires it off.

The symptoms you described—sometimes getting "email sent" but receiving nothing, and other times seeing a generic "my server is not setup" error—are classic indicators of an incorrectly configured mailer transport or missing headers. The CodeIgniter Email library does not just call mail(); it prepends headers like From, Reply-To, and X-Mailer based on your configuration file. If your application/config/email.php settings are pointing to a non-existent SMTP host or an invalid port, the library will attempt to connect, fail, and return false.

Inspecting the Configuration First

Before diving into complex scripts, you must verify the settings in application/config/email.php. The most common failure point is the $protocol variable. By default, CodeIgniter often defaults to 'mail' or attempts to auto-detect based on your server setup. However, if your server requires specific SMTP credentials (common with shared hosting or cloud environments like AWS SES), the default settings will likely cause a connection timeout or a protocol error.

Check the following variables in your config:

  • $protocol: Set this explicitly to 'smtp' if you are using an external mail service. If you intend to use the system's sendmail, ensure the path is correct.
  • $host: The SMTP host address. If this is blank or points to a local server that isn't running Postfix or Exim, the connection will fail.
  • $user and $pass: These are required for SMTP authentication. Even if you think they aren't needed, many modern hosting providers require SASL authentication.
  • $smtp_timeout: The default might be too short for slower networks.

If your code outputs "my server is not setup," it usually means the library tried to parse headers and failed because the underlying mail() function couldn't establish a valid connection. This often happens when the sendmail_path in php.ini is misconfigured, or the system mail queue is full.

Debugging the Transport Layer

In your controller logic, you are correctly using $this->email->print_debugger(). This is crucial because it reveals why the send failed. If the debugger shows a connection error to an SMTP host, you know immediately that the protocol or host settings in application/config/email.php are wrong.

If the debugger shows no specific error but returns false, check the headers being generated. CodeIgniter modifies the email headers based on the $from method. If your config file has a hardcoded sendmail_path that points to /usr/sbin/sendmail -t -i (a common default), but your server doesn't have sendmail installed or configured correctly, the library will fail while the raw mail() function might bypass some of these checks depending on how it is invoked.

The Reliability Gap Between mail() and CodeIgniter

It is tempting to fall back to mail() when the library fails, but this is a dangerous pattern. Raw mail() sends an email with minimal headers and relies entirely on the server's default configuration. It lacks the robustness of the CodeIgniter Email class, which handles attachment encoding (MIME types), proper character set handling for UTF-8, and DKIM/SPF header additions if configured.

If you switch to mail() because the library fails, you are essentially ignoring the abstraction benefits of the framework. The "sometimes it works" behavior suggests a race condition or a transient connection issue that the library's retry logic or proper SMTP handshake would handle gracefully. Using raw mail() without checking headers means your emails might land in spam folders because the Message-ID or Content-Type headers are malformed.

Recommended Fix

The most reliable fix is to explicitly configure the Email library for SMTP in application/config/email.php. Remove any reliance on auto-detection:

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'your-smtp-host.com'; // e.g., smtp.gmail.com
$config['smtp_user'] = 'your-email@domain.com';
$config['smtp_pass'] = 'your-password';
$config['smtp_port'] = 465; // or 587 for TLS
$config['smtp_timeout'] = 120;
$config['smtp_crypto'] = 'tls'; // or 'ssl'

Once these are set, clear your CodeIgniter cache and retry the send. If the library still fails, inspect application/logs for any PHP errors occurring before the mail() call is reached. Often, the error lies in a missing dependency (like OpenSSL not being installed) rather than the email logic itself.

Our network