Troubleshooting Gmail SMTP setup with CodeIgniter email library

I need help setting up Gmail SMTP in CodeIgniter’s email library as I’m facing connection issues. Here’s how my code looks:

<?php
class EmailSender extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('email');
    }

    public function index()
    {
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'ssl://smtp.gmail.com';
        $config['smtp_port'] = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user'] = '[email protected]';
        $config['smtp_pass'] = 'yourpassword';
        $config['charset'] = 'utf-8';
        $config['newline'] = "\r\n";
        $config['mailtype'] = 'text';
        $config['validation'] = TRUE;

        $this->email->initialize($config);
        $this->email->from('[email protected]', 'Your Name');
        $this->email->to('[email protected]');
        $this->email->subject('Test Email');
        $this->email->message('This is a test to see if the email works.');

        $this->email->send();
        echo $this->email->print_debugger();
        
        $this->load->view('email_sent_view');
    }
}

When I try to use port 465, I encounter a timeout error from fsockopen(). If I switch to ports 25 or 587, I receive SSL errors. I prefer to use CodeIgniter’s email library instead of PHPMailer. What changes should I make for it to work correctly?

Your config needs two quick fixes. Drop the ssl:// prefix from smtp_host - just use smtp.gmail.com. Then add $config['smtp_crypto'] = 'ssl'; for port 465. I’ve run this setup for years without problems. Here’s what catches everyone: don’t use your regular Gmail password. Go to your Google Account security settings and generate an App Password instead. Also bump smtp_timeout to 30 - Gmail’s often slow to respond. That fsockopen timeout usually means bad credentials or Gmail’s blocking your connection.

I had the same issue with Gmail SMTP in CodeIgniter. Beyond just port settings, check your Gmail account first. You’ll need to either allow less secure apps or (better option) enable 2-step verification and create an App Password. Port 587 with TLS (‘smtp_crypto’ set to ‘tls’) fixes most SSL errors. Also make sure Gmail isn’t blocking your app’s sign-in attempts - their security settings can kill the connection.

Switch to port 587 and use $config['smtp_crypto'] = 'tls'; instead of ssl. Drop the ssl:// from your host config - CodeIgniter handles encryption separately. Gmail blocks regular passwords now, so you’ll need an app-specific password from Google’s security settings. Fixed my timeout issues after days of troubleshooting.