Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created July 22, 2025 19:25
Show Gist options
  • Save thinkphp/06c451b9e9ebe270a5fa33f7de10b1eb to your computer and use it in GitHub Desktop.
Save thinkphp/06c451b9e9ebe270a5fa33f7de10b1eb to your computer and use it in GitHub Desktop.
contact.php
<?php
// Set sendmail_from address explicitly to ensure the correct "From" address
ini_set('sendmail_from', '[email protected]');
// 1. Define recipient email, subject, and "From" address
$to = "[email protected]"; // Replace with the recipient's email address
$subject = "New Contact Form Submission";
$from = "[email protected]"; // Specify the "From" email address
// 2. Collect form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Check for empty fields and display an error if any are missing
if (empty($name) || empty($email) || empty($message)) {
echo "Please fill in all fields correctly.";
exit;
}
// 3. Prepare the email content
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// 4. Set email headers with "From" and "Reply-To" addresses
$headers = "From: $from\r\n";
$headers .= "Reply-To: $email\r\n"; // Set the Reply-To to the user's email address
// 5. Send the email with the `-f` option to specify the "From" address
if (mail($to, $subject, $email_content, $headers, "-f $from")) {
echo "Thank you for contacting us, $name. We will get back to you shortly.";
} else {
echo "Sorry, there was an issue sending your message.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment