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

PHP发送邮箱验证时SMTP端口连接失败问题求助

Fixing "mail(): Failed to connect to mailserver at localhost port 25" in XAMPP

Hey there, I’ve dealt with this exact error multiple times when setting up email functionality in XAMPP. Let’s break down the fixes step by step:

1. Start XAMPP’s Built-in Mail Server

XAMPP comes with Mercury Mail (its default mail server), but it’s usually not running by default. Here’s how to enable it:

  • Open your XAMPP Control Panel.
  • Look for the Mercury Mail module (sometimes labeled just "Mail Server").
  • Click the Start button. If it fails to start, port 25 is likely being used by another program (like Windows’ built-in SMTP service or antivirus software). To fix this:
    • Open Command Prompt as admin and run netstat -ano | findstr :25 to find the PID of the process using port 25.
    • Open Task Manager, go to the Details tab, find that PID, and end the process. Then try starting Mercury Mail again.

2. Update Your php.ini Configuration

Even if Mercury is running, your PHP settings might not be pointing to it correctly:

  • Navigate to C:\xampp\php\php.ini and open it in a text editor.
  • Search for the [mail function] section. Update these lines (uncomment them if they’re commented out with ;):
    SMTP = localhost
    smtp_port = 25
    sendmail_from = your-test-email@localhost.com
    
  • Save the file, then restart the Apache module in XAMPP to apply the changes.

3. Use a Third-Party SMTP Service (More Reliable Alternative)

If you don’t want to mess with the local mail server, using a real SMTP provider (like Gmail, Outlook, or SendGrid) is often more reliable. Note that native mail() doesn’t support SSL/TLS directly, so I recommend using PHPMailer (a popular PHP library for sending emails) instead—it handles encryption and authentication smoothly.

Here’s a quick example with PHPMailer and Gmail:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php'; // Make sure you've installed PHPMailer via Composer

$mail = new PHPMailer(true);
try {
    // Server settings
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your-gmail-address@gmail.com';
    $mail->Password = 'your-app-password'; // Use an app password if 2FA is enabled
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    // Recipients
    $mail->setFrom('your-gmail-address@gmail.com', 'Your Name');
    $mail->addAddress('recipient@example.com');

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'Email Verification';
    $mail->Body = 'Your verification link here';

    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

4. Quick Debugging Checks

  • After making changes, always restart Apache to ensure new settings take effect.
  • Check Mercury Mail’s logs (found in C:\xampp\mercury\logs) for any specific errors if the server won’t start.
  • Test with a simple mail() call first to rule out script-specific issues:
    mail('test@example.com', 'Test Subject', 'Test Body');
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:45:16