I've been trying to create a simple contact form using PHP. So, I've managed to get the form to work by plugging in values for all variables like so and it sends the email just fine:
$to = "andrew@mingbogu.com";
$subject = "Job";
$message = "Test";
$from = "example@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
However, as soon as I replace the values with $_REQUEST[var], the email gets sent to my Junk email with no from address:
$to = "andrew@mingbogu.com";
$subject = "Job";
$message = $_REQUEST['clientMessage'];
$from = $_REQUEST['clientEmail'];
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$headers .= "From: $from";
mail($to,$subject,$message,$headers);
Screenshot of the email:
I'm getting the values through an ajax form using jQuery:
var clientName = $("#name").val();
window.alert(clientName);
var clientEmail = $("#email").val();
window.alert(clientEmail);
var clientMessage = $("#message").val();
window.alert(clientMessage);
var data = "from=" + clientEmail + "&subject=" + clientName + "&message=" + clientMessage;
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function() {
$("#loading").fadeOut(100).hide();
$("#name").val(' ');
$("#email").val(' ');
$("#message").val(' ');
$("#message-sent").fadeIn(100).show();
}
});
I've printed out clientName
, clientMessage
, and clientEmail
all right before sending them into the ajax form and they all print out with correct values. Any idea what I'm doing wrong?
HTML form:
<form id="contactMe" data-abide>
<input type="text" id="name">
<input type="text" id="email">
<textarea id="message"></textarea>
<button type="submit" id="sendEmail" class="button">Submit</button>