PHP mail函数发送邮件成功但收件人未收到问题求助
Hey there, let's break down why your form shows success but the emails never reach the recipient. PHP's mail() function returning true only means the server accepted the request to send—it doesn't guarantee delivery. Here are the most common fixes for this issue:
1. Fix the Email Headers (Critical for Deliverability)
Most shared hosting servers block emails sent from arbitrary "From" addresses (like the user's submitted email). They require the From address to be a valid, existing email on your domain (e.g., noreply@xxx.pl). Instead, use the Reply-To header to let you respond to the user's email directly.
Also, adding proper content type and encoding prevents spam filters from flagging your message as suspicious.
Modified Headers Example:
// Build the email headers with proper domain-based From and Reply-To $email_headers = "From: No Reply <noreply@xxx.pl>\r\n"; $email_headers .= "Reply-To: $name <$email>\r\n"; $email_headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
2. Check for Spam Filtering
Even if the server sends the email, it might end up in the recipient's spam folder. Test by sending to your own email first and check both the inbox and spam folder.
To reduce spam flags:
- Avoid overly casual or spammy language in the subject line or message content
- Ensure your domain has valid SPF and DKIM records set up (these tell email providers your server is authorized to send mail for your domain)
3. Verify Server Mail Logs (If You Have Access)
If your hosting plan lets you access server logs, check the mail logs (usually in paths like /var/log/mail.log). These logs will show exactly what happened to the email—whether it was sent, rejected by the recipient's server, or flagged as spam.
4. Test with a Minimal Mail Script
Rule out form-related issues by creating a simple test script to send an email directly:
<?php $to = "yyy@xxx.pl"; $subject = "Test Email"; $message = "This is a test from PHP mail()"; $headers = "From: No Reply <noreply@xxx.pl>\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; if(mail($to, $subject, $message, $headers)) { echo "Mail sent successfully"; } else { echo "Mail failed"; } ?>
If this test email works, the issue might be tied to form data processing (though your original code looks solid). If it doesn't, the problem is definitely server/hosting-related.
Modified Full PHP Code (With Fixes)
Here's your original PHP code updated with the header fixes:
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the form fields, removes html tags and whitespace. $name = strip_tags(trim($_POST["name"])); $name = str_replace(array("\r","\n"),array(" "," "),$name); $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); $message = trim($_POST["message"]); // Check the data. if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) { header("Location: http://www.xxx.pl/index.php?success=-1#contact"); exit; } $recipient = "yyy@xxx.pl"; $subject = "New contact from $name"; // Build the email content. $email_content = "Name: $name\n"; $email_content .= "Email: $email\n\n"; $email_content .= "Message:\n$message\n"; // Fixed headers with domain-based From and proper encoding $email_headers = "From: No Reply <noreply@xxx.pl>\r\n"; $email_headers .= "Reply-To: $name <$email>\r\n"; $email_headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; // Send the email. $mail_sent = mail($recipient, $subject, $email_content, $email_headers); // Only redirect if mail was accepted by server (though delivery isn't guaranteed) if($mail_sent) { header("Location: http://www.xxx.pl/index.php?success=1#contact"); } else { header("Location: http://www.xxx.pl/index.php?success=-1#contact"); } } else { echo "There was a problem with your submission, please try again."; } ?>
One last thing: Make sure noreply@xxx.pl is a real, existing email on your domain (most hosts let you create these easily in cPanel or your hosting dashboard).
内容的提问来源于stack exchange,提问作者Kaghar

