Robert Robert - 1 year ago 70
Javascript Question

php not sending email from contact form

I am trying to set up a simple email function in php. Looking through the other questions that address the same issue, it appears that I'm doing the same thing the answers are saying to do. However, when I try to send the email, it doesn't send.


<form id="contactMeForm">
<div class="formField">
<label for="senderName">Name:</label>
<input type="text" id="senderName" class="textField" placeholder="Your name" />
<div class="formField">
<label for="senderEmail">Email:</label>
<input type="text" id="senderEmail" class="textField" placeholder="Your email" />
<div class="formField">
<label for="contactReason">Contact reason:</label>
<select id="contactReason">
<option>I'd like a quote for a website.</option>
<div class="formField" id="message">
<div id="senderPhone">
<label for="returnPhone">Contact number:</label>
<input type="text" id="returnPhone" class="textField" placeholder="Best phone number to reach you at." />
<label for="senderMessage">Message:</label>
<textarea id="senderMessage"></textarea>


type: "POST",
url: "scripts/email.php",
data: {name: senderName, email: senderEmail, reason: contactReason, phone: returnPhone, message: senderMessage},
dataType: "json",
success: function(response){ = "none"; = "block"; = "block";
buttons[1].style.display = "none";
contactMeHeader[0].textContent = "Your message has been sent.";


$name = $_POST["name"];
$email = $_POST["email"];
$reason = $_POST["reason"];
$phone = $_POST["phone"];
$headers = "From: " .$email ."\r\n";
$message = $_POST["message"];

mail("", $reason, $message, $headers);
echo json_encode(array($name, $email, $reason, $phone, $message));


[mail function]
; For Win32 only.
smtp_port = 25

I'm running an Apache server with EasyPHP on a Windows machine. I'm also not getting any error messages.

Answer Source

I'm guessing none of the fields are populating. You need to add the name property to each of the fields, that's what PHP is looking for in the $_POST array.

<input type="text" id="senderEmail" class="textField" placeholder="Your email" name="email" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download