Bootstrap php contact form: php wont sent email
Posted: Sat Apr 15, 2017 9:53 pm
Hi everyone.
I've been struggling for quite awhile on getting a bootstrap contact form to send email.
1. I use bootstrap form validation
2. I use fontawesome
3. I use phpmailer for sending email.
The form looks good, its the way I want it and the bootstrap validation works. The captcha validation works too.. It seems that my php code doesn't run at all, no success or failure messages display and no email is sent. I'd appreciate it if someone could point me in the right direction. I haven't had this issue before.
I've been struggling for quite awhile on getting a bootstrap contact form to send email.
1. I use bootstrap form validation
2. I use fontawesome
3. I use phpmailer for sending email.
The form looks good, its the way I want it and the bootstrap validation works. The captcha validation works too.. It seems that my php code doesn't run at all, no success or failure messages display and no email is sent. I'd appreciate it if someone could point me in the right direction. I haven't had this issue before.
Code: Select all
<?php
ob_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Starter Template for Bootstrap</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="css/starter-template.css" rel="stylesheet">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- bootstrap handles validation so php validation error checks not needed-->
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/css/bootstrapValidator.min.css"/>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js"> </script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script src="js/validate-me.js"></script>
</head>
<body>
<div class="container">
<form class="well form-horizontal" action="" method="post" id="contact_form">
<fieldset>
<legend>Contact Us Today!
<?php
if(isset($_POST['submit'])) {
echo 'submit button clicked';
$FullName = isset($_POST['fullname']) ? trim($_POST['fullname']) : '';
$EmailAddress = isset($_POST['email_address']) ? trim($_POST['email_address']) : '';
$CompanyPosition = isset($_POST['company_position']) ? trim($_POST['company_position']) : '';
$CompanyName = isset($_POST['company_name']) ? trim($_POST['company_name']) : '';
$PhoneNumber = isset($_POST['phone_number']) ? trim($_POST['phone_number']) : '';
$Country = isset($_POST['country']) ? trim($_POST['country']) : '';
$Location = isset($_POST['location']) ? trim($_POST['location']) : '';
$ServicesRequired = isset($_POST['services_required']) ? trim($_POST['services_required']) : '';
$CasesPerYear = isset($_POST['cases_per_year']) ? trim($_POST['cases_per_year']) : '';
$Comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
if(isset($_POST['g-recaptcha-response'])) {$captcha=$_POST['g-recaptcha-response']; }
$errors = array();
//if (empty($_POST['full_name']) ) {$errors[]='Full Name Required';}
//if (empty($_POST['company_position']) ) { $errors[]='Company Position Required'; }
//if (empty($_POST['company_name']) ) {$errors[]='Company Name Required';}
//if (empty($_POST['phone_number']) ) {$errors[]='Phone Number Required'; }
//if (empty($_POST['email_address'])) {$errors[]='Email Address Required';}
//if (empty($_POST['country']) ) {$errors[]='Country Required'; }
//if (empty($_POST['location']) ) {$errors[]='Location Required'; }
//if (empty($_POST['services_required']) ) {$errors[]='Services Requires is Required'; }
//if (empty($_POST['cases_per_year']) ) {$errors[]='Cases Per Year is Required'; }
//if (empty($_POST['comment']) ) {$errors[]='Comment is Required'; }
if(!$captcha){ $errors[]='<div class="alert alert-danger" role="alert" id="captcha_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i> The Captcha code was invalid.</div>'; }
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LennxwUAAAAADsh9_YK-7A2EAILA35i5mN_tEpj&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false) {
echo '<div class="alert alert-danger" role="alert" id="captcha_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i> The Captcha code was invalid.</div>';
} else {
if (empty($errors)) {
$to = 'cjkeane@shaw.ca';
$ContactEmail = 'cjkeane@shaw.ca';
$subject = 'RE: Request A Quote submitted from megassistance.com';
$message = "
<html>
<body>
<table rules='all' style='border-color: #666;' cellpadding='10' width='697' >
<tr><td colspan='2'><h1><strong>Megassistance.com: Request A Quote</strong></h1></td></tr>
<tr><td colspan='2'><h2 style='background: #eaf1dd; color:#660066'><strong>Contact Information</strong> for:</h2></td></tr>
<tr><td width='323'> Name</td><td width='374'>$FullName</td></tr>
<tr><td>Email Address</td><td>$EmailAddress</td></tr>
<tr><td>Job Position</td><td>$JobPosition</td></tr>
<tr><td>Company Name</td><td>$CompanyName</td></tr>
<tr><td> Phone Number</td><td>$PhoneNumber</td></tr>
<tr><td>E-Mail Address</td><td>$ContactEmail</td></tr>
<tr><td>Country</td><td>$Country</td></tr>
<tr><td>Location</td><td>$Location</td></tr>
<tr><td>Services Required</td><td>$ServicesRequired</td></tr>
<tr><td>Number of Cases Per Year</td><td>$CasesPerYear</td></tr>
<tr><td>Comments:</td><td>$Comments</td></tr>
</table>
</body>
</html>";
require_once 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsHTML(true);
$mail->From = '$EmailAddress';
//$mail->FromName = 'cjkeane@shaw.ca';
$toaddresses = 'cjkeane@shaw.ca';
$mail->Subject = "RE: Request A Quote";
$mail->Body = $message;
if(!$mail->Send()) {
echo '<div class="alert alert-danger" role="alert" id="failure_message">Message Failed <i class="glyphicon glyphicon-thumbs-down"></i>>Sorry there was an error sending your message. </div><br/>';
} else {
echo '<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> Thanks for contacting us, we will get back to you shortly.</div><br />';
}
} else {
echo '<div class="alert alert-danger" role="alert" id="failure_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i>>Sorry there were validation errors. Please try again.</div>';
}
}
}
?>
</legend>
<div class="form-group">
<label class="col-md-4 control-label">Full Name</label>
<div class="col-xs-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input name="full_name" placeholder="Name" class="form-control" id="ContactName" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-xs-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input name="email_address" placeholder="E-Mail Address" class="form-control" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Job Position</label>
<div class="col-xs-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
<input name="company_position" placeholder="Position" class="form-control" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Company Name</label>
<div class="col-xs-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
<input name="company_name" placeholder="Company Name" class="form-control" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Phone Number</label>
<div class="col-xs-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
<input name="phone" placeholder="(845)555-1212" class="form-control" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Your Location</label>
<div class="col-xs-6 selectContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
<select name="location" class="form-control selectpicker">
<option value=" " >Please select your location </option>
<option>Canada</option>
<option>USA</option>
<option >Latin America</option>
<option >Europe</option>
<option >Africa</option>
<option >Asia</option>
<option >Middle East</option>
<option >Others</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Services Required</label>
<div class="col-xs-6 selectContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
<select name="services_required" class="form-control selectpicker" multiple="multiple">
<option value="1">Outpatient visits (housecall, urgent care, walk in clinic, emergency room)</option>
<option value="2">Inpatient visits (hospitals)</option>
<option value="3">Repatriation</option>
<option value="4">Costs containment</option>
<option value="5">Others</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Number of cases per year</label>
<div class="col-xs-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
<input name="cases_per_year" placeholder="Number of cases per year" class="form-control" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Your Comment</label>
<div class="col-xs-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" name="comment" placeholder="Comment"></textarea>
</div>
</div>
</div>
<!-- Success/Fail message -->
<?php echo $result; ?>
</fieldset>
<label class="col-md-4 control-label"></label>
<div class="col-md-4"><div class="g-recaptcha" data-sitekey="6LennxwUAAAAAG7YqFDJQ2GvbzZulMNuTdKHcQOW"></div></div>
<div class=clearfix></div>
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<br /> <input type="submit" name="submit" id="submit" class="btn btn-warning">
</div>
</div>
</form>
</div>
<?php
ob_end_flush();
?>
</body>
</html>