Robert Robert - 2 months ago 8
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.

HTML

<form id="contactMeForm">
<div class="formField">
<label for="senderName">Name:</label>
<input type="text" id="senderName" class="textField" placeholder="Your name" />
</div>
<div class="formField">
<label for="senderEmail">Email:</label>
<input type="text" id="senderEmail" class="textField" placeholder="Your email" />
</div>
<div class="formField">
<label for="contactReason">Contact reason:</label>
<select id="contactReason">
<option></option>
<option>I'd like a quote for a website.</option>
</select>
</div>
<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." />
</div>
<label for="senderMessage">Message:</label>
<textarea id="senderMessage"></textarea>
</div>



JS

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


PHP

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

mail("robbyt15@gmail.com", $reason, $message, $headers);
echo json_encode(array($name, $email, $reason, $phone, $message));
?>


php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25


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

Answer

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" />
Comments