PHP多字段联系表单提交后无法发送邮件问题排查求助
Hey Jessica, let's break down why your multi-field contact form isn't delivering emails—even though your single-field version works. Since you're not seeing errors and already checked spam folders, here are the most likely fixes to test:
1. Verify if the mail() Function Actually Succeeded
Right now, you're showing the "thank you" message no matter whether mail() worked. The function returns a boolean (true/false) that tells you if the server accepted the mail request. Update your PHP code to validate this:
<?php if($_POST["submit"]) { $recipient="myemail@gmail.com"; $subject="Contact Form"; $sender=trim($_POST["sender"]); $senderEmail=trim($_POST["senderEmail"]); $message=trim($_POST["message"]); $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message"; // Capture the mail() result to check success $mailSent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"); if($mailSent) { $thankYou="<p>Thank you! Your message has been sent.</p>"; } else { $thankYou="<p>Oops! There was an error sending your message.</p>"; // Log the failure for later debugging (check your server error logs) error_log("Mail failed for sender: $senderEmail | Subject: $subject"); } } ?>
If you start seeing the error message, that confirms mail() is failing on the server side.
2. Fix Your Email Header Formatting
The From header in your code might be invalid if the user enters special characters (like commas, quotes, or non-ASCII letters) in their name. This can cause mail servers to reject the email silently.
Also, many hosting providers block emails where the From address isn't tied to your server's domain (e.g., using a user's Gmail address directly). Instead, use a domain-specific "no-reply" address for the sender, and set a Reply-To header so you can respond to the user:
// Validate and sanitize inputs first $sender = htmlspecialchars(trim($_POST["sender"])); $senderEmail = filter_var(trim($_POST["senderEmail"]), FILTER_VALIDATE_EMAIL); if(!$senderEmail) { $thankYou="<p>Please enter a valid email address.</p>"; } else { // Format headers correctly with proper encoding $headers = "From: Contact Form <no-reply@yourdomain.com>\r\n"; $headers .= "Reply-To: " . mb_encode_mimeheader($sender) . " <$senderEmail>\r\n"; $headers .= "Content-Type: text/plain; charset=utf-8\r\n"; $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message"; $mailSent = mail($recipient, $subject, $mailBody, $headers); // ... rest of your success/error handling }
mb_encode_mimeheader()ensures special characters in the sender's name are encoded properly.- Using
no-reply@yourdomain.commatches your server's domain, avoiding spam filters that flag "spoofed" sender addresses.
3. Include All Form Fields (and Sanitize Them)
Your form has extra fields (last, title, business, phone) that aren't being added to the email body. While this doesn't directly break delivery, unfiltered input in these fields could contain characters that corrupt the email structure (like line breaks that inject extra headers).
Sanitize all inputs and add them to your mail body:
$last = htmlspecialchars(trim($_POST["last"])); $title = htmlspecialchars(trim($_POST["title"])); $business = htmlspecialchars(trim($_POST["business"])); $phone = htmlspecialchars(trim($_POST["phone"])); $mailBody = "Name: $sender $last\n" . "Title: $title\n" . "Business: $business\n" . "Phone: $phone\n" . "Email: $senderEmail\n\n" . $message;
htmlspecialchars() escapes special characters, preventing them from breaking the email format.
4. Check Your Server's Mail Logs
Even if mail() returns true, the email might be rejected by your server's mail service or the recipient's provider. Most hosting providers log mail activity:
- On Linux servers, check
/var/log/mail.logor/var/log/mail.err(you may need cPanel/SSH access). - On shared hosting, look for a "Mail Logs" section in your control panel.
Logs will show details like:
- "SPF check failed" (your server isn't authorized to send emails from your domain)
- "Relay access denied" (your hosting provider blocks outbound mail from non-domain addresses)
- "Message rejected as spam" (content or headers triggered a filter)
5. Test with a Minimal Email
To rule out content issues, send a super basic email first:
$mailSent = mail($recipient, "Test Email", "This is a test message", "From: no-reply@yourdomain.com");
If this test email arrives, the problem is with your form's input handling or header formatting. If it doesn't, your server's mail configuration is the issue—contact your hosting provider for help.
内容的提问来源于stack exchange,提问作者Jessica

