Codeigniter部署至AWS EC2后无法发送邮件问题求助
Hey there! Let's figure out why your CodeIgniter emails stopped sending after moving your domain to AWS EC2—since it worked on your old server, the issue is almost certainly tied to EC2's environment or configuration. Here are the most common fixes to test out:
1. Check AWS EC2's Default SMTP Port Restrictions
AWS blocks port 25 (a common default for SMTP) on most EC2 instances to prevent spam. Instead, use port 587 (TLS) or 465 (SSL) which are allowed. You can set this in your CodeIgniter code dynamically:
$this->email->set_port(587); $this->email->set_crypto('tls'); // Use 'ssl' if you're using port 465
Or add these settings permanently in config/email.php:
$config['smtp_port'] = 587; $config['smtp_crypto'] = 'tls';
2. Verify EC2 Security Group & Network ACL Rules
Make sure your EC2 security group allows outbound traffic on the SMTP port you're using (587/465):
- Go to your EC2 console → Security Groups → Select your instance's security group
- Add an outbound rule: Protocol = TCP, Port Range = 587 (or 465), Destination = 0.0.0.0/0 (or your email provider's specific IP range)
- Double-check your VPC Network ACLs too—they should also allow outbound traffic on these ports.
3. Fix SMTP Authentication & Provider Restrictions
If you're using a third-party SMTP service (like Gmail, SendGrid, or your domain's mail server):
- Credentials: Ensure your SMTP username/password are correct. For services like Gmail with 2FA enabled, you'll need to use an App Password instead of your regular account password.
- IP Whitelisting: Many email providers flag new EC2 IPs as suspicious. Log into your email provider's dashboard and add your EC2 instance's public IP to their whitelist.
- Domain Authentication: Set up SPF, DKIM, and DMARC records for your domain in Route 53 (AWS's DNS service). This proves your server is authorized to send emails from your domain, reducing the chance of being marked as spam.
4. Explicitly Configure SMTP in CodeIgniter
Your old server might have had default SMTP settings that EC2 doesn't. Add these details before sending your email to ensure CodeIgniter uses the correct SMTP server:
$this->email->protocol = 'smtp'; $this->email->smtp_host = 'your-smtp-server.com'; // e.g., smtp.gmail.com or mail.yourdomain.com $this->email->smtp_user = 'your-email@yourdomain.com'; $this->email->smtp_pass = 'your-app-password-or-email-password';
You can also add these to config/email.php so you don't have to repeat them in every email function.
5. Debug with CodeIgniter's Email Debugger
The fastest way to pinpoint the exact issue is to print CodeIgniter's email error details. Modify your code like this:
if (!$this->email->send()) { echo $this->email->print_debugger(); }
This will show you specific errors like "SMTP connection failed", "Authentication rejected", or "Timeout reached"—which will tell you exactly what to fix.
6. Check SELinux & System Firewall
If you're using Amazon Linux or a RHEL-based distro, SELinux might be blocking PHP from sending emails. Temporarily disable it to test:
sudo setenforce 0
If emails start sending, enable the correct SELinux rule permanently:
sudo setsebool -P httpd_can_sendmail on
Also, make sure the system firewall (iptables) allows outbound traffic on your SMTP port.
7. Set Up Reverse DNS (PTR Record)
Some strict email providers require a reverse DNS record for your EC2 IP to accept emails. To do this:
- Assign an Elastic IP to your EC2 instance
- Open an AWS Support ticket requesting a PTR record that points your Elastic IP to your domain (e.g.,
mail.yourdomain.com)
Start with the debugger step first—it'll save you time by pointing straight to the problem. Most folks fix this with port changes or security group adjustments!
内容的提问来源于stack exchange,提问作者Shihas

