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

CodeIgniter邮件投递异常问题:VPS服务器上代码返回发送成功但收件人未收到邮件

Hey there, let's figure out why your CodeIgniter email is showing as sent but not arriving on that specific VPS. Since it works locally and on other servers, the issue is almost certainly tied to the VPS's environment or security settings. Here's a step-by-step breakdown to diagnose and fix this:

1. First, Grab the Full SMTP Debug Log

CodeIgniter's send() method might return true even if there's an underlying SMTP error that's not being surfaced. Right after calling $this->email->send(), add this line to get detailed debug info:

echo $this->email->print_debugger();

This will show you the full conversation between your server and Gmail's SMTP server. Look for error codes like 535 Authentication Failed (bad credentials/security settings) or Connection refused (port blocked). This is the single most useful step to narrow down the issue.

2. Check if the VPS Can Reach Gmail's SMTP Port

Your config uses port 465 (SSL) for Gmail. It's possible the VPS's firewall, network provider, or security group is blocking outgoing traffic on this port. Test connectivity directly on the VPS using these commands:

  • For telnet: telnet smtp.googlemail.com 465
  • For netcat: nc -zv smtp.googlemail.com 465
    If you get a "Connection refused" or timeout, you need to open port 465 in your VPS's firewall (e.g., ufw allow 465/tcp for Ubuntu) or update your cloud provider's security group rules.

3. Verify Your Gmail Account Security Settings

Gmail is strict about unfamiliar login locations, which might block your VPS's attempts to send mail:

  • If you have 2-Step Verification (2FA) enabled on your Gmail account, you can't use your regular password. You need to create an App Password (found in your Google Account > Security > App Passwords) and use that in smtp_pass.
  • If you don't have 2FA enabled, make sure "Less secure app access" is turned on (note: Google is phasing this out, so App Password is the better long-term solution).
    Also, check your Gmail's "Recent activity" page to see if there's a blocked login attempt from your VPS's IP address—you might need to approve it.

4. Fix a Minor Config Inconsistency

Your current config uses ssl://smtp.googlemail.com (port 465) with starttls => true, but STARTTLS is meant for port 587 (TLS upgrade). While Gmail might tolerate this, it's better to align the settings properly to avoid unexpected issues:

  • Option 1 (keep port 465): Set starttls => false
  • Option 2 (use port 587): Change smtp_host to tcp://smtp.googlemail.com, smtp_port to 587, and keep starttls => true

5. Check Server Mail Service (When Using protocol => 'mail')

When you switched to protocol => 'mail', CodeIgniter uses the server's local mail service (like Sendmail or Postfix). Make sure:

  • The mail service is running: systemctl status postfix (for Postfix) or systemctl status sendmail
  • Check the server's mail logs for errors: /var/log/mail.log (Debian/Ubuntu) or /var/log/maillog (CentOS/RHEL). These logs will tell you if the mail was rejected by the local service or remote servers.

6. Rule Out SELinux Restrictions (For RHEL/CentOS-Based VPS)

If your VPS uses SELinux (common on CentOS), it might be blocking PHP from making outgoing network connections. Check if SELinux is enforcing:

getenforce

If it returns Enforcing, temporarily disable it to test:

setenforce 0

If the email sends successfully after this, you'll need to allow PHP to send mail permanently:

setsebool -P httpd_can_sendmail on

(Use php-fpm_can_sendmail instead if you're using PHP-FPM.)

7. Verify DNS Resolution on the VPS

Make sure the VPS can correctly resolve Gmail's SMTP domain. Run this command:

nslookup smtp.googlemail.com

Compare the IPs returned to what you get locally. If they don't match or the command fails, your VPS has a DNS issue—try changing your DNS servers to Google's (8.8.8.8 and 8.8.4.4) in /etc/resolv.conf.

Start with the debug log first—it'll point you directly to the root cause most of the time. Let me know what you find!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 08:42:42