You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.conf has the correct handler for .php files (like AddHandler 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 using http://localhost with 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 disable mail() by default, or require you to use their specific SMTP credentials.
  • Missing/incorrect email headers: Without proper headers (like From or Reply-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(): The header("Location: thank-you.html"); function must run before any other output (including spaces, HTML, or even blank lines before the <?php tag). Even a single space will break the redirect.
  • Incorrect redirect path: Double-check that thank-you.html is 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 add exit; right after the header() 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.php with this code:
    <?php phpinfo(); ?>
    
    Access it via your server (e.g., 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.php to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:57:52