Sending email with gmail smtp with codeigniter email library

Sending email with gmail smtp with codeigniter email library

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

Stefan Bogdanescu

Founder & Senior Architect

Debugging Gmail SMTP Connectivity in CodeIgniter

Attempting to send email via the built-in CodeIgniter email library often triggers frustrating SSL errors when configuring Gmail as the transport. The specific error fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection timed out) indicates that the socket connection is failing before an SSL handshake can even occur. This usually stems from a mismatch between how modern PHP handles TLS connections and the legacy configuration options available in the framework's email library.

Gmail enforces strict security policies on SMTP access. To resolve this without switching to third-party libraries like PHPMailer, you must align your CodeIgniter configuration with Gmail's current requirements. The most critical factor is the protocol version and port combination. While Port 465 implies implicit SSL (STARTTLS was historically used there), modern PHP versions often struggle connecting to ssl://smtp.gmail.com on port 465 because they attempt an SSL handshake immediately, which Gmail sometimes rejects or times out against if the client isn't configured precisely.

A more reliable configuration for CodeIgniter's native email library involves using Port 587 with STARTTLS. In this setup, the connection starts unencrypted and then upgrades to TLS once the server responds. You should update your configuration array to reflect this:

$config['protocol']    = 'smtp';
$config['smtp_host']    = 'smtp.gmail.com'; // Remove 'ssl://' prefix for Port 587
$config['smtp_port']    = 587;              // Use STARTTLS port
$config['smtp_timeout'] = 30;               // Increase timeout to allow for handshake
$config['smtp_user']    = 'your-email@gmail.com';
$config['smtp_pass']    = 'your-app-specific-password'; // See below
$config['charset']      = 'UTF-8';
$config['newline']      = "\r\n";

Removing the ssl:// prefix from the host is a common fix when switching ports. If you explicitly set the port to 587, PHP will attempt STARTTLS automatically if supported by the underlying OpenSSL extension. However, the persistent error SSL operation failed with code 1 suggests that the SSL context provided by CodeIgniter might not be negotiating the cipher suite correctly, or the host string is incorrect.

Another frequent pitfall involves authentication. Gmail no longer accepts standard application passwords for most accounts unless 2-Factor Authentication (2FA) is enabled. If you have 2FA turned on, the $config['smtp_pass'] field must contain a specific "App Password" generated in your Google Account security settings, not your main login password. Using the wrong credentials will result in authentication errors, but sometimes these manifest as connection timeouts if the handshake fails immediately upon receiving an invalid login response.

You should also verify that your PHP OpenSSL extension is correctly installed and up to date. If you are running an older version of CodeIgniter (3.x), the library has known limitations regarding complex SSL handshakes. In such cases, updating to a newer CodeIgniter version or migrating to a modern stack like Laravel might be necessary. While switching frameworks is a significant undertaking, the native email library in CodeIgniter lacks many features present in dedicated libraries, making it less robust for production environments requiring high reliability.

If you decide to stick with CodeIgniter, ensure that your mailtype is set correctly (text or html) and that validation is enabled if needed. However, the core issue here is almost always a combination of host string formatting and port selection. By switching to Port 587, removing the ssl:// prefix, and ensuring you are using an App Password with 2FA enabled, you can usually bypass the SSL timeout errors without needing to integrate external libraries like PHPMailer or SwiftMailer.

For developers looking for a more robust email solution within the Laravel ecosystem, resources such as https://laravelcompany.com provide extensive documentation on handling SMTP configurations that are often more forgiving than CodeIgniter's legacy email class. If you find yourself constantly fighting SSL errors in an older framework, leveraging these modern best practices or migrating to a newer stack is often the most pragmatic path forward.

Our network