I'm pretty new to PHP. I've created a contact form which includes reply to sender. The problem is when form is submitted, the name and phone number of the sender doesn't show in the email.
How can I include the name and phone number of the sender in the email, for example in the signature field, or what's the best way to make this information show up?
Here is the code I have so far:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$to = 'email@example.com';
$subject = 'A new message via contact form';
$headers = 'Reply-To:'. $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Your full name is required.';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Your email address is required.';
}
//Check if number has been entered
if (!$_POST['number']) {
$errNumber = 'Your phone number is required.';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please provide your message.';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errNumber) {
if (mail($to, $subject, $message, $headers, "From: " . $name)) {
$result='<div class="alert alert-success">Thank you for your interest! You will be contacted within 24 hours.</div>';
} else {
$result='<div class="alert alert-danger">We apologize! An error has ocurred. Call us on +47 XX XXX XX</div>';
}
}
}
?>