You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter发送邮件至Gmail无法接收问题求助

Fixing CodeIgniter Email Delivery Issues to Gmail

Hey there! Let’s figure out why your CodeIgniter emails are landing in your company’s inbox just fine, but getting blocked by Gmail. Gmail has strict security measures for SMTP access, so here are the most reliable fixes to get your emails through:

1. Use a Google App Password (Critical if 2FA is Enabled)

If you have two-factor authentication (2FA) turned on for your Gmail account, your regular password won’t work for SMTP connections. You’ll need to create an App Password instead:

  • Log into your Google Account, head to the Security section
  • Under "Signing in to Google", select 2-Step Verification (make sure it’s enabled)
  • Scroll down to App Passwords, pick "Mail" as the app, and "Other (custom name)" as the device
  • Generate the password, then replace your smtp_pass value with this 16-character code (no spaces!)

2. Add Encryption Configuration

Your current setup uses port 465 (SSL), but CodeIgniter needs an explicit encryption setting to connect properly. Add this line to your config:

$config['smtp_crypto'] = 'ssl';

Alternatively, you can switch to port 587 with TLS encryption (sometimes more reliable for Gmail):

$config['smtp_port'] = 587;
$config['smtp_crypto'] = 'tls';

3. Check for Gmail Security Blocks

If you still can’t send, Google might have flagged your SMTP attempt as an unusual sign-in. Check your Gmail inbox for a security alert email—you’ll need to click "Allow access" to let your CodeIgniter app connect.

4. Optimize Settings to Avoid Spam Filters

Gmail’s spam filters are tough, so tweak these settings to improve deliverability:

  • Make sure your from email matches your smtp_user (Gmail doesn’t allow spoofed senders)
  • Add these config lines to boost formatting and authenticity:
    $config['mailtype'] = 'html'; // Or 'text' if you're sending plain text
    $config['charset'] = 'utf-8';
    $config['newline'] = "\r\n"; // Required for SMTP compliance
    

Full Working Config Example

Here’s your updated config with all the fixes included:

$this->load->library('email');
$config['useragent'] = 'CodeIgniter';
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.googlemail.com';
$config['smtp_user'] = 'example@gmail.com';
$config['smtp_pass'] = 'your-generated-app-password'; // Replace with your App Password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = 30;
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";

$this->email->initialize($config);

// Set email content
$this->email->from('example@gmail.com', 'Your Display Name');
$this->email->to('recipient@gmail.com');
$this->email->subject('Test Email from CodeIgniter');
$this->email->message('This should now land in your Gmail inbox!');

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

If you run into issues, use print_debugger() to get detailed error messages—it’ll tell you if the problem is a connection failure, invalid credentials, or something else.

内容的提问来源于stack exchange,提问作者Usama Iftikhar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:32:22