PHP mailer , send via Ajax
HTML:
<form action="<?php bloginfo('template_url'); ?>/sendemail.php" method="post">
<div class="form-group">
<label>Full Name</label>
<input type="hidden" id="car_type" name="car_type" value="<?php the_field('car_name') ; ?>">
<input type="text" name="fullname" id="fullname" class="form-control">
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" name="phone" id="phone" class="form-control">
</div>
<input class="btn blackbg" type="submit" name="submitButton" id="cta_submit_single" value="SUBMIT">
</form>
<div class="success alert alert-info sform" style="display: none;"></div>
PHP that sends email for this form
<?php
require 'PHPMailerAutoload.php';
$fullname = $_POST['fullname'] ;
$car_type = $_POST['car_type'] ;
$phone = $_POST['phone'] ;
$email = $_POST['email'] ;
$from = $_POST['from'] ;
$to = $_POST['to'] ;
/*
echo "VALUES SENT TO EMAIL:<br>" ;
echo '<pre>';
print_r($_POST) ;
echo '</pre>';
exit;
*/
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "john@doe.com"; // GMAIL username
$mail->Password = "pass123#";
$mail->setFrom('john@doe.com', 'test Mailer');
$mail->addReplyTo('john@doe.com', 'Information');
$mail->addAddress('john@doe.com', 'J.C'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Reserve Request: ' . $car_type;
$mail->Body = "
<strong>Full Name:</strong> $fullname<br>
<strong>Subject:</strong> $car_type<br>
<strong>Phone:</strong> $phone <br>
<strong>From:</strong> $from<br>
<strong>To:</strong> $to" ;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
JAVASCRIPT:
<script>
jQuery(function() {
jQuery("#cta_submit_single").click(function() {
var data = {
fullname: jQuery("#fullname").val(),
car_type: jQuery("#car_type").val(),
phone: jQuery("#phone").val(),
email: jQuery("#email").val(),
from: jQuery("#from").val(),
to: jQuery("#to").val()
};
jQuery.ajax({
type: "POST",
url: "<?php bloginfo('template_url'); ?>/sendemail.php",
data: data,
success: function(msg){
jQuery('.success.alert.alert-info.sform').css("display", "block").text(msg);
console.log("MESSAGE DETAILS: " + msg ) ;
},
error: function(msg){
jQuery('.success.alert.alert-info.sform').text("Error");
console.log("ERROR MESSAGE: " + msg) ;
}
});
return false;
});
});
</script>