Configuring CodeIgniter to send emails via Gmail SMTP on a local server

Hey everyone, I’m pulling my hair out trying to get CodeIgniter to send emails using Gmail SMTP on my localhost. I’ve tried a bunch of different settings but no luck so far. Here’s what I’ve got:

$mailConfig = [
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.gmail.com',
    'smtp_port' => 465,
    'smtp_user' => '[email protected]',
    'smtp_pass' => 'mypassword',
    'mailtype' => 'html',
    'charset' => 'utf-8',
    'smtp_crypto' => 'ssl',
    'smtp_auth' => true
];

$this->load->library('email', $mailConfig);
$this->email->from('[email protected]', 'Sender Name');
$this->email->to('[email protected]');
$this->email->subject('Test Email');
$this->email->message('This is a test email');

if (!$this->email->send()) {
    echo $this->email->print_debugger();
} else {
    echo 'Email sent successfully';
}

I keep getting authentication errors. I’ve already enabled less secure app access in my Google account and tried the captcha unlock thing. Still no dice. Any ideas what I’m missing? Also, how would I set this up once I move to a live server? Thanks for any help!

Have you considered using PHPMailer instead of CodeIgniter’s built-in email library? It’s more robust and often easier to configure for Gmail SMTP. Here’s a quick example:

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your-app-password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;

$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

This approach has worked well for me on both local and live servers. Just remember to use an app password and ensure your server allows outgoing SMTP connections.

try using port 587 instead of 465 and change smtp_crypto to ‘tls’. for gmail, you might need to generate an app password in your google account settings. that’s worked for me before. on a live server, make sure your hosting provider allows outgoing smtp connections. good luck!

I’ve been there, mate. Gmail can be a real pain when it comes to SMTP. Here’s what worked for me:

First off, ditch the regular password and set up 2-factor authentication on your Google account. Then generate an App Password specifically for your CodeIgniter app. It’s more secure and usually fixes those pesky auth errors.

Also, check your firewall settings. Sometimes they block outgoing SMTP connections, especially on localhost. You might need to whitelist the Gmail SMTP server.

On a live server, I’d recommend using a transactional email service like SendGrid or Mailgun instead of Gmail. They’re more reliable and designed for this kind of thing. Plus, you won’t hit Gmail’s sending limits.

Hope this helps! Let us know how you get on.