网站联系表单邮件发件人显示异常,无法回复求助
Hey, let's get this sorted out for you. The issue has two key causes: a bug in your PHP code, plus Hostinger's email server restrictions that are overriding your sender address. Here's how to fix both:
1. Fix the PHP Code Errors
Your current code has an undefined variable ($full_name) that's breaking the sender format, and you're not setting up the email headers correctly to ensure replies work. Here's the corrected code with explanations:
<?php /* Set your domain email as the recipient */ $myemail = "info@mydomain.com"; // Sanitize and validate user input $yourname = check_input($_POST['yourname'], "Enter your name"); $subject = check_input($_POST['subject'], "Write a subject"); $user_email = check_input($_POST['email'], "Enter your email address"); $comments = check_input($_POST['comments'], "Write your comments"); // Validate email format if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $user_email)) { show_error("E-mail address not valid"); } // Build the message content (clean line breaks for readability) $message = "Hello! You have received a message from your website: Name: $yourname Subject: $subject E-mail: $user_email Message: $comments End of message "; // Set proper email headers: // - Use YOUR domain email as the sender (avoids Hostinger's override) // - Set Reply-To to the user's email so replies go directly to them $headers = 'From: "Website Contact Form" <' . $myemail . '>' . "\r\n" . 'Reply-To: "' . $yourname . '" <' . $user_email . '>' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); // Send the email mail($myemail, $subject, $message, $headers); // Redirect to thank you page header('Location: thanks.html'); exit(); // Input sanitization helper function function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } // Error display function function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?>
Key fixes in this code:
- Removed the undefined
$full_namevariable and replaced it with the user's input name ($yourname) for proper formatting - Set the
Fromaddress to your own domain email (this avoids Hostinger's automatic sender override for unapproved addresses) - Ensured
Reply-Tois set to the user's actual email, so clicking "reply" in your Hostinger webmail will send the message directly to them - Added a required check for the user's email input
2. Configure Hostinger DNS Records (Critical!)
Hostinger's shared hosting servers block custom sender addresses by default to prevent spam. To get your sender address to show correctly, you need to add SPF and DKIM records to your domain's DNS:
- SPF Record: Add a TXT record with these details:
- Hostname:
@ - Value:
v=spf1 include:spf.hostinger.com ~all
- Hostname:
- DKIM Record:
- Log into your Hostinger hPanel
- Go to Emails > Email Accounts
- Find your domain, click Manage > DKIM
- Copy the provided DKIM value and add it as a TXT record with hostname
default._domainkey
Wait 1-24 hours for DNS changes to propagate (usually takes 1-2 hours for most providers). After this, Hostinger will recognize your domain email as a valid sender and won't override it.
Why This Works
- The fixed code ensures your email headers are properly formatted, so replies go to the right place even if Hostinger temporarily overrides the sender during DNS propagation
- The SPF/DKIM records authenticate your domain, letting Hostinger know you're authorized to send emails from your domain address
内容的提问来源于stack exchange,提问作者Guy Randalf

