PHP网站表单数据邮件发送脚本开发及收信故障排查
Troubleshooting & Fixing Your PHP Form Email Issue
Let’s dive into why your form isn’t sending emails and how to fix it—your core problem is a misuse of PHP’s mail() function, plus a couple of small syntax glitches.
First: Fix the Critical mail() Function Error
PHP’s mail() function follows a strict structure: mail($to, $subject, $message, $headers, $params). You’re passing $name, $choice, and $date as separate parameters, which the function doesn’t recognize. Instead, you need to bundle all that form data into a single $message string.
Here’s the corrected code block for the email-sending logic:
} else { $emailTo = "my@addressemail.com"; $subject = "trial session"; $name = $_POST['name']; $choice = $_POST['choice']; $date = $_POST['date']; $senderEmail = $_POST['email']; // Combine all form data into one readable message $message = "Name: " . $name . "\r\n"; $message .= "Sender Email: " . $senderEmail . "\r\n"; $message .= "Trial Session Choice: " . $choice . "\r\n"; $message .= "Selected Date: " . $date . "\r\n"; // Add proper headers to avoid spam flags and ensure formatting $headers = "From: " . $senderEmail . "\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; // Correct mail() function call if (mail($emailTo, $subject, $message, $headers)) { $successMessage = '<div class="alert alert-success" role="alert">Your message was sent, we\'ll get back to you ASAP!</div>'; } else { $error = '<div class="alert alert-danger" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</strong></p></div>'; } }
Fix Other Small Syntax Issues
- Form Action Quotation Marks: Your form uses Chinese double quotes
”email.php”—replace these with English double quotes"email.php"so the browser can correctly parse the submission URL. - Short PHP Tags: You’re using
<? echo $error.$successMessage; ?>—some servers disable short tags by default. Switch to full tags:<?php echo $error . $successMessage; ?>to ensure compatibility across all environments.
Additional Troubleshooting Steps
If fixing the code above still doesn’t resolve the issue, try these:
- Test a Minimal
mail()Script: Create a simple test file to rule out server-level problems:
If this fails, your server isn’t configured to send emails. For local environments (XAMPP/WAMP), you’ll need to set up sendmail or use an SMTP service. For shared hosting, contact your provider to confirm the<?php error_reporting(E_ALL); ini_set('display_errors', 1); $to = "your-test-email@example.com"; $subject = "Test Email"; $message = "This is a test message from your server."; $headers = "From: test@yourdomain.com\r\n"; if(mail($to, $subject, $message, $headers)) { echo "Email sent successfully!"; } else { echo "Failed to send email. Check server logs for details."; } ?>mail()function is enabled. - Check Spam Folders: Emails sent via the native
mail()function often get flagged as spam—double-check your webmail’s spam/junk folder. - Enable PHP Error Logging: Add
error_reporting(E_ALL); ini_set('display_errors', 1);at the top of youremail.phpto reveal hidden errors that might be silenced by default. - Switch to PHPMailer: The native
mail()function is unreliable and prone to spam filters. PHPMailer supports SMTP authentication (using services like Gmail, SendGrid, or your host’s SMTP server) and is far more dependable for form emails.
内容的提问来源于stack exchange,提问作者student24
相关产品推荐
相关产品推荐

