PHP联系表单无法发送邮件求助:无报错却未收到邮件
Hey there! Since your previous similar template worked, this is almost certainly a small, fixable oversight—let’s walk through the most common reasons your form redirects successfully but doesn’t send emails:
1. Missing or Malformed Email Headers
Most mail servers will reject emails that don’t have proper From: or Reply-To: headers, and this is the #1 culprit for silent failures. Your old template might have included these, but your new one might not.
For example, instead of just calling mail($to, $subject, $message), you need to add headers like this:
$headers = "From: No Reply <noreply@your-domain.com>\r\n"; // Use a domain-specific email (critical for shared hosts) $headers .= "Reply-To: " . $_POST['user_email'] . "\r\n"; // Let you reply to the user $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
Shared hosting providers often require the From: address to be tied to your website’s domain (e.g., noreply@your-site.com), not the user’s input email—otherwise, it gets flagged as spoofing.
2. Not Checking the mail() Function’s Return Value
PHP’s mail() function doesn’t throw errors by default—it just returns true or false. If you’re redirecting immediately without verifying if the email sent, you’ll never know it failed. Add this check:
$mailSent = mail($to, $subject, $message, $headers); if (!$mailSent) { // Log the error for debugging (check your server's error logs later) error_log("Failed to send email to $to from " . $_POST['user_email']); // Temporarily show an error to test (remove this in production) die("Oops! There was a problem sending your message."); } // Only redirect if the email actually sent header("Location: success.php"); exit;
3. Server Mail Configuration Limits
If you’re on shared hosting, some providers disable or restrict the native mail() function. Your old template might have used SMTP (like PHPMailer or SwiftMailer) instead of the default mail()—which is more reliable for sending emails.
If mail() keeps failing, consider switching to an SMTP library: it handles authentication, avoids server restrictions, and gives you better error messages.
4. Email Being Marked as Spam
Even if the email sends, it might land in the recipient’s spam folder. Check there first! To reduce this risk:
- Include clear, non-generic subject lines (e.g., "Contact Form Submission from John Doe" instead of "New Message")
- Add the user’s full message and contact details so the email has meaningful content
- Use a domain-specific
From:address as mentioned earlier
5. Redirecting Before the Email Sends
Double-check that your header("Location: ...") line comes after the mail() call. If you redirect before mail() executes, the script stops, and the email never gets sent.
Quick Working Example
Here’s a trimmed-down version of what your code should look like to cover these bases:
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Sanitize form data (always do this!) $name = htmlspecialchars(trim($_POST['name'])); $email = htmlspecialchars(trim($_POST['email'])); $message = htmlspecialchars(trim($_POST['message'])); $to = "your-target-email@example.com"; $subject = "New Contact Form Submission: $name"; $body = "Name: $name\nEmail: $email\n\nMessage:\n$message"; $headers = "From: No Reply <noreply@your-domain.com>\r\n"; $headers .= "Reply-To: $email\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; $mailSent = mail($to, $subject, $body, $headers); if ($mailSent) { header("Location: success.php"); exit; } else { error_log("Mail failed for $name <$email>"); echo "Sorry, we couldn't send your message right now. Please try again later."; } } ?>
Start with checking the headers and the mail() return value first—those are the easiest fixes. If that doesn’t work, check your server’s error logs (most hosts have a section for this in cPanel or their dashboard) to see why the email was rejected.
内容的提问来源于stack exchange,提问作者JMDev




