Can anyone review my code please and tell me why the email is not sending? I'm attempting to write a jQuery Ajax method that posts contact form data to a PHP email script.
Here is the body of the contact form page:
<body>
<?php require 'Includes/Header.php' ?>
<div id="container1">
<h1>Contact Us</h1>
<form id="form" onSubmit="return validate(this);" method="post" action="customerMsg2.php" >
<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="FirstName" id="FirstName" autofocus></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="LastName" id="LastName"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="Email" id="Email"></td>
</tr>
<tr>
<td>Leave a message: </td>
<td><textarea name="Message" id="Message"></textarea></td>
</tr>
<tr>
<td><input type="submit" id="sendEmail" value="Send Email"></td>
</tr>
<input type="hidden" name="submitted" value="true">
</table>
</form>
<div id="ajax-message"></div>
</div>
<div id="container2">
<ul id="slideshow">
<li id="services"><h2>Make a Donation!</h2><br>
Our pets need lots of love and attention while they wait for new homes. Why not give today?<br><input type="button" value="Donate"></li>
<li id="services"><h2>Check Out Our Podcast!</h2><br>
Tune in weekly as Josh gives his unique perspective on pet-ownership and dutifully regulated capitalism.</li>
</ul>
</div>
<script src="slideshow.js"></script>
<script src="ajaxsubmit.js"></script>
<?php require 'Includes/Footer.php' ?>
</body>
</html>
Here is the jQuery, ajax code (ajaxsubmit.js):
$(document).ready(function () {
var $form = $('#form');
var ajaxmessage = $('#ajax-message');
$form.submit(function (event) {
event.preventDefault();
var serializedData = $form.serialize();
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: serializedData
})
.done(function (response) {
$(ajaxmessage).removeClass('error');
$(ajaxmessage).addClass('success');
$(ajaxmessage).text(response);
$('#FirstName').val('');
$('#LastName').val('');
$('#Email').val('');
$('#Message').val('');
})
.fail(function (data) {
$(ajaxmessage).removeClass('success');
$(ajaxmessage).addClass('error');
if (data.responseText !== '') {
$(ajaxmessage).text(data.responseText);
} else {
$(ajaxmessage).text('Oops! An error occured and your message could not be sent.');
}
});
});
});
and here is the PHP script:
<?php
ini_set('SMTP', 'smtp.gmail.com');
$to = '(i would put my email address here)';
$firstname = strip_tags(trim($_POST["FirstName"]));
$lastname = strip_tags(trim($_POST["LastName"]));
$message = trim($_POST["Message"]);
$from = trim($_POST['Email']);
$subject = "New contact from $firstname $lastname";
if(mail($to, $subject, $message, $from))
{
echo "Message sent.";
}
else
{
echo "Message not sent.";
}
?>
Any ideas?
This is an example from php doc:
`<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "
" .
'Reply-To: webmaster@example.com' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Your header info is not correct.
Bye
I did not check all your code but the PHP script has several problems:
First the mail function is defined as follows:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
You can't just put an email address string as $additional_headers
.
For example:
$header = "From: " . $from . "
";
mail($to, $subject, $message, $header);
Further I guess that you can't send emails with a different from header as your email address over the gmail server. Normally your hosting provider has already configured the needed settings for sending emails with the php mail function.