PHPMailer报错‘EOF caught while checking if connected’问题求助
Hey there, let's break down what might be causing that EOF error with your internal SMTP setup and how to fix it:
First, let's recap your issue
You had PHPMailer working fine with your internal SMTP server (192.168.10.184:25) a week ago, but now you're seeing EOF caught while checking if connected the following from address failed. Swapping to Gmail's SMTP works, so the problem lies with your internal server or how PHPMailer interacts with it.
Step-by-step troubleshooting & fixes
1. Fix invalid PHPMailer configuration values
Looking at your code, there's a wrong parameter value that could be breaking the connection:
- You set
$mail->SMTPSecure = 'non';— PHPMailer doesn't recognize'non'as a valid option. For unencrypted port 25, use an empty string instead:$mail->SMTPSecure = ''; - Double-check if your internal SMTP server requires authentication. If it never did, leaving
$mail->SMTPAuth = true;commented out is fine, but if it was recently updated to require auth, you'll need to uncomment that line and use valid internal server credentials (not your Gmail credentials).
2. Address the "from address failed" clue
The error calls out your from address as a problem, which is likely the root cause here:
- Your current setup uses the user-submitted email (
$_POST['email']) as theFromaddress. Most internal SMTP servers block external/non-whitelisted from addresses to prevent spam. - Test with a trusted internal from address first, like
noreply@yourlocaldomain.com, then keep usingAddReplyTo()to route replies back to the user (you already have this set up correctly!).
3. Add more debugging and timeout settings
- Crank up
SMTPDebugto2instead of1— this will show you full raw communication with the SMTP server, which can reveal exactly where the connection is failing. - Add a timeout to avoid premature connection drops:
$mail->Timeout = 30;
4. Verify your internal SMTP server is accessible
Even if you say the host/port is correct, it's worth double-checking:
- From your web server, run a test command to confirm connectivity:
telnet 192.168.10.184 25 # Or if telnet isn't available: nc -zv 192.168.10.184 25 - Check the internal SMTP server's logs (e.g., Postfix, Exim logs) for entries about rejected connections or invalid from addresses.
- Ensure firewalls (on both the web server and SMTP server) aren't blocking traffic on port 25 between the two machines.
Test code with recommended adjustments
Here's a modified version of your code with the fixes above to test:
<?php if(isset($_POST["submit"])) { $from = $_POST['email']; $name = $_POST['name']; include("phpmailer/class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); $mail->CharSet = 'UTF-8'; $mail->SMTPDebug = 2; // Full debug output for troubleshooting $mail->Host = '192.168.10.184'; $mail->SMTPAuth = false; // Set to true if your internal server requires auth $mail->Port = 25; // Uncomment and fill if auth is required: // $mail->Username = 'internal_smtp_user@yourdomain.com'; // $mail->Password = 'internal_smtp_password'; $mail->SMTPSecure = ''; // Correct value for unencrypted port 25 $mail->Timeout = 30; // Prevent timeout-related EOFs // Use an internal, allowed from address $mail->From = 'noreply@yourlocaldomain.com'; $mail->FromName = $name; $mail->isHTML(true); $mail->Subject = $_POST['subject']; $mail->Body = $_POST['message']; $to = "xyz3@gmail.com"; $mail->addAddress($to); $mail->AddReplyTo($from, $name); // Keep this for user replies if($mail->Send()) { echo '<script type="text/javascript"> window.location = "http://localhost/abcwebsite/" </script>'; } else { // Show error info for debugging (remove in production) echo "Mailer Error: " . $mail->ErrorInfo; // echo '<script type="text/javascript"> window.location = "http://localhost/abcwebsite/"</script>'; } } ?>
The debug output from SMTPDebug=2 should give you clear clues if the server is rejecting your from address, refusing the connection, or returning other errors.
内容的提问来源于stack exchange,提问作者Shahnawaz Khan Niazi

