HTML表单提交后PHP邮件未发送且显示PHP文件内容问题排查
Hey there! Let's break down why your form isn't working as expected and fix it step by step.
1. First & Most Critical Issue: PHP File Shows as Plain Text
When your browser displays the raw PHP code instead of running it, that means your server isn't set up to parse PHP files. Here are the most common reasons:
- No PHP environment installed: If you're using a static-only hosting plan (which only supports HTML/CSS/JS), it won't run PHP. You'll need a host with PHP support, or set up a local server like XAMPP/WAMP for testing.
- Incorrect server configuration: For Apache, check that
httpd.confhas the correct handler for.phpfiles (likeAddHandler application/x-httpd-php .php). For Nginx, make sure your config forwards PHP requests to PHP-FPM. - Accessing files directly via
file://: If you're double-clicking the HTML file to open it in your browser (instead of usinghttp://localhostwith a local server), your browser can't parse PHP code at all.
2. Why the Email Isn't Sending (Once PHP is Fixed)
Once your server can run PHP, the mail function might still fail for these reasons:
- No mail server configured: PHP's
mail()function relies on a local mail service (like Sendmail or Postfix) or SMTP settings. Many shared hosts disablemail()by default, or require you to use their specific SMTP credentials. - Missing/incorrect email headers: Without proper headers (like
FromorReply-To), your email might get flagged as spam, or the server might refuse to send it. - Form data not being captured: If you misspelled a field name (e.g., using
$_POST['Name']instead of$_POST['name']), the email content will be empty, and the send might silently fail.
3. Why the Thank-You Page Isn't Loading
If PHP runs but the redirect doesn't work, check these:
- Output before
header(): Theheader("Location: thank-you.html");function must run before any other output (including spaces, HTML, or even blank lines before the<?phptag). Even a single space will break the redirect. - Incorrect redirect path: Double-check that
thank-you.htmlis in the same directory as your PHP file, or use an absolute path (like/thank-you.html) if it's in your root folder. - Forgot
exit;after redirect: Always addexit;right after theheader()call—otherwise, the rest of your PHP code might run, and the redirect might not trigger properly.
Quick Troubleshooting Steps
- Test your PHP environment: Create a file named
info.phpwith this code:
Access it via your server (e.g.,<?php phpinfo(); ?>http://yourdomain.com/info.php). If it shows a PHP info page, your environment is working. If it shows raw code, fix the server setup first. - Enable error reporting: Add these lines at the top of your
form-to-email.phpto see exact errors:<?php error_reporting(E_ALL); ini_set('display_errors', 1); // rest of your code ?> - Test a simple mail script: Use this minimal script to rule out form-related issues:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); $to = 'your-email@example.com'; $subject = 'Test Email'; $message = 'This is a test from your form'; $headers = 'From: no-reply@yourdomain.com' . "\r\n" . 'Reply-To: no-reply@yourdomain.com' . "\r\n"; if(mail($to, $subject, $message, $headers)) { header("Location: thank-you.html"); exit; } else { echo "Failed to send email. Check your server's mail settings."; } ?>
内容的提问来源于stack exchange,提问作者Nicholas McKinney
相关产品推荐
相关产品推荐

