PHP发送邮箱验证时SMTP端口连接失败问题求助
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 :25to 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.
- Open Command Prompt as admin and run
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.iniand 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

