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

使用CodeIgniter PHP发送邮件时SMTP连接失败警告问题求助

Fixing CodeIgniter mail() Connection Error to localhost:25

Hey there, that warning you're seeing is super common when working with CodeIgniter's Email library locally—let's get this sorted out. The core issue is that your local environment doesn't have a functioning SMTP server running on port 25 of localhost, which the default mail() function relies on. Here are a few solid solutions:

1. Switch CodeIgniter to use an external SMTP service (most reliable for local dev)

Instead of relying on your local server, use a third-party SMTP provider like Gmail, SendGrid, or Mailgun. Update your Email controller (at C:\xampp\htdocs\prmadmin\application\controllers\Email.php) with this configuration before calling send():

// Load the email config first
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com'; // Replace with your provider's host
$config['smtp_port'] = 587;
$config['smtp_user'] = 'your-email-address@gmail.com';
$config['smtp_pass'] = 'your-app-specific-password'; // For Gmail, use an app password, not your regular password
$config['smtp_crypto'] = 'tls';
$config['mailtype'] = 'html'; // Or 'text' if you don't need HTML emails
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n"; // Critical for some SMTP servers

// Initialize the library with the config
$this->load->library('email', $config);

// Set up your email details
$this->email->from('your-email@gmail.com', 'Your Name');
$this->email->to('recipient@example.com');
$this->email->subject('Test Email from CodeIgniter');
$this->email->message('This should work now!');

// Send and debug if needed
if ($this->email->send()) {
    echo "Email sent successfully!";
} else {
    // Print debugger to see detailed errors
    echo $this->email->print_debugger();
}

2. Configure php.ini to use a working SMTP server

If you prefer using the mail() function directly, update your php.ini file (found in C:\xampp\php\php.ini) with valid SMTP settings:

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from=your-email@gmail.com

After making these changes, restart your Apache server from the XAMPP control panel for the settings to take effect.

3. Use XAMPP's built-in Mercury Mail Server

XAMPP comes with a local mail server called Mercury. Here's how to set it up:

  • Open your XAMPP control panel, find the "Mercury" module, and click "Start" to launch it.
  • Verify that Mercury is running on port 25 (you can check this in Mercury's settings).
  • If you still get errors, you can temporarily override the SMTP settings in your code using ini_set():
    ini_set('SMTP', 'localhost');
    ini_set('smtp_port', 25);
    
    Place this code at the top of your Email controller before loading the Email library.

Why this happens

By default, PHP's mail() function tries to connect to a local SMTP server on port 25. Most local development environments (like XAMPP) don't have this server running or configured out of the box, which triggers that connection warning.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:29:13