PHP邮件无法发送表单核心数据:邮件内容为空,寻求技术支持
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
actionshould 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 themail()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_logor your hosting control panel) to catch issues like missing files (e.g.,geoplugin.class.phpnot found). - Validate geoplugin: Ensure the
geoplugin.class.phpfile exists and is correctly included—if it's missing, that could break your script before it sends the email.
内容的提问来源于stack exchange,提问作者Daniel

