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

PHP邮件无法发送表单核心数据:邮件内容为空,寻求技术支持

Troubleshooting Empty PHP Mail() Form Submissions

Hey Daniel, let's work through why your form data isn't making it to your email and fix this issue. From what you've shared, the core problems are missing form fields (name, email, phone) in the submission and empty email content—here's how to address both:

1. First: Fix Form Data Retrieval

The most common reason form data doesn't pass through is missing or incorrect name attributes on your HTML inputs, or not validating the request method.

Check Your HTML Form

Make sure every input you want to capture has a name attribute (not just an id). Example:

<form method="POST" action="your-mail-script.php">
    <label>Name:</label>
    <input type="text" name="full_name" required> <!-- Critical: name="full_name" -->
    
    <label>Email:</label>
    <input type="email" name="contact_email" required>
    
    <label>Phone:</label>
    <input type="tel" name="contact_phone" required>
    
    <button type="submit">Send</button>
</form>
  • Ensure the form uses method="POST" (matches how you'll retrieve data in PHP).
  • The action should point directly to your PHP mail script.

Safely Fetch Data in PHP

Never assume form data exists—use isset() or null coalescing to avoid undefined index errors, and clean the input:

// Only process POST requests
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Safely get and sanitize form data
    $name = isset($_POST['full_name']) ? trim($_POST['full_name']) : 'Not provided';
    $email = isset($_POST['contact_email']) ? filter_var(trim($_POST['contact_email']), FILTER_VALIDATE_EMAIL) : 'Invalid email';
    $phone = isset($_POST['contact_phone']) ? trim($_POST['contact_phone']) : 'Not provided';
}

2. Fix Empty Email Content

Your mail() function is likely sending empty content because you're not appending the retrieved form data to the message body. Here's a complete, fixed script:

<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();

// Correct IP address retrieval (fix your truncated code)
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // Handle multiple IPs in X-Forwarded-For
    $ip = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]);
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get sanitized form data
    $name = isset($_POST['full_name']) ? trim($_POST['full_name']) : 'Not provided';
    $email = isset($_POST['contact_email']) ? filter_var(trim($_POST['contact_email']), FILTER_VALIDATE_EMAIL) : 'Invalid email';
    $phone = isset($_POST['contact_phone']) ? trim($_POST['contact_phone']) : 'Not provided';

    // Build the email message with all collected data
    $to = 'your-recipient-email@example.com'; // Replace with your email
    $subject = 'New Form Submission from ' . $name;
    $message = "You received a new form submission:\n\n";
    $message .= "Name: " . $name . "\n";
    $message .= "Email: " . $email . "\n";
    $message .= "Phone: " . $phone . "\n";
    $message .= "Browser: " . $browser . "\n";
    $message .= "IP Address: " . $ip . "\n";
    $message .= "Location: " . $geoplugin->city . ", " . $geoplugin->countryName . "\n";

    // Set headers to avoid spam filters and enable replies
    $headers = "From: " . $email . "\r\n";
    $headers .= "Reply-To: " . $email . "\r\n";
    $headers .= "Content-Type: text/plain; charset=utf-8\r\n";

    // Send email and check for errors
    if (mail($to, $subject, $message, $headers)) {
        echo "Thank you! Your message has been sent.";
    } else {
        // Debug: Show error details
        echo "Oops, something went wrong. Error: " . print_r(error_get_last(), true);
    }
} else {
    // Reject non-POST requests
    echo "Invalid request. Please submit the form through our website.";
}
?>

3. Additional Troubleshooting Tips

  • Check if mail() is enabled: Some hosting providers disable the mail() function. If you get persistent errors, switch to an SMTP library like PHPMailer (more reliable and avoids spam issues).
  • Check spam folders: Emails sent via mail() often end up in spam—add your recipient email to your whitelist.
  • Review PHP error logs: Look for errors in your server's error log (usually in error_log or your hosting control panel) to catch issues like missing files (e.g., geoplugin.class.php not found).
  • Validate geoplugin: Ensure the geoplugin.class.php file exists and is correctly included—if it's missing, that could break your script before it sends the email.

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:06:05