Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Show errors
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- //Load Required Components
- require_once 'src/recaptcha_autoload.php';
- require_once "functions.php";
- use PHPMailerPHPMailerPHPMailer;
- use PHPMailerPHPMailerException;
- require 'src/Exception.php';
- require 'src/PHPMailer.php';
- require 'src/SMTP.php';
- $errorsMSG = array(); // array to hold validation errors
- $data = array(); // array to pass back data
- // validate the variables ======================================================
- // if any of these variables don't exist, add an error to our $errorsMSG array
- if (empty($_POST['firstName'])) $errorsMSG['firstName'] = 'First Name is required.';
- if (empty($_POST['lastName'])) $errorsMSG['lastName'] = 'Last Name is required.';
- if (empty($_POST['companyName'])) $errorsMSG['companyName'] = 'Company Name is required.';
- if (empty($_POST['companyAddress'])) $errorsMSG['companyAddress'] = 'Company Address is required.';
- if (empty($_POST['city'])) $errorsMSG['city'] = 'City is required.';
- // if (empty($_POST['state'])) $errorsMSG['state'] = 'State is required.';
- if (empty($_POST['emailAddress'])) $errorsMSG['emailAddress'] = 'Email Address is required.';
- if (empty($_POST['message'])) $errorsMSG['message'] = 'Message is required.';
- // return a response ===========================================================
- // if there are any errors in our errors array, return a success boolean of false
- if (!empty($errorsMSG)) {
- // if there are items in our errors array, return those errors
- $data['success'] = false;
- $data['errorsMSG'] = $errorsMSG;
- } else {
- checkRecaptcha();
- }
- function checkReCaptcha()
- {
- // Declare Variables
- $gRecaptchaResponse = $_POST['g-recaptcha-response'];
- $remoteIp = get_client_ip();
- $secret = 'SECRET_KEY';
- // form submit check
- if (isset($_POST['submit']))
- {
- // Google reCAPTCHA response check
- if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
- {
- // Create an instance of the service using your secret
- $recaptcha = new ReCaptchaReCaptcha($secret);
- $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
- // check if response is a success
- if ($resp->isSuccess())
- {
- //Send Email
- sendMail();
- }
- else
- {
- //Throw Error if Validation Failed
- $errMsg = 'Robot verification failed, please try again.';
- };
- }
- else
- {
- //Throw Error if User Forgot
- $errMsg = 'Please click on the reCAPTCHA box.';
- };
- }
- //End of Check Google Recaptcha Function
- }
- function sendMail() {
- $mail = new PHPMailer(true); // Passing `true` enables exceptions
- try {
- //Server settings
- $mail->SMTPDebug = 2; // Enable verbose debug output
- $mail->isSMTP(); // Set mailer to use SMTP
- $mail->Host = 'smtp.server.com'; // Specify main and backup SMTP servers
- $mail->SMTPAuth = true; // Enable SMTP authentication
- $mail->Username = 'user@server.com'; // SMTP username
- $mail->Password = 'SECRET_PASSWORD'; // SMTP password
- $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
- $mail->Port = 587; // TCP port to connect to
- //Recipients
- $mail->setFrom('user@server.com', 'Mailer');
- $mail->addAddress('user@server.com', 'Joe User'); // Add a recipient
- //Content
- $mail->isHTML(true); // Set email format to HTML
- $mail->Subject = 'New Message from Contact Form';
- //prepare email body
- $body_message = "";
- $body_message .= "Sender IP: " . get_client_ip() ."<br>";
- $body_message .= "First Name: " . $firstName ."<br>";
- $body_message .= "Last Name: " . $lastName ."<br>";
- $body_message .= "Company Name: " . $companyName ."<br>";
- $body_message .= "Company Address: " . $companyAddress ."<br>";
- $body_message .= "City: " . $city ."<br>";
- $body_message .= "State: " . $state ."<br>";
- $body_message .= "Sender email: " . $emailAddress ."<br>";
- $body_message .= "Sender Phone: " . $phoneNumber ."<br>";
- $body_message .= "nn". $message;
- $mail->Body = $body_message;
- $mail->send();
- $errorsMSG = 'Message successfully sent.';
- }
- catch(Exception $e) {
- $mail->ErrorInfo;
- $errorsMSG = 'There has been an issue sending your message.';
- }
- //End of sendMail Function
- }
- // return all our data to an AJAX call
- echo json_encode($data);
- ?>
- // Start
- $(document).ready(function() {
- // process the form
- $('form').submit(function(event) {
- $('.form-group').removeClass('has-error'); // remove the error class
- $('.help-block').remove(); // remove the error text
- // get the form data
- // there are many ways to get this data using jQuery (you can use the class or id also)
- var formData = {
- 'firstName' : $('input[name=firstName]').val(),
- 'lastName' : $('input[name=lastName]').val(),
- 'companyName' : $('input[name=companyName]').val(),
- 'companyAddress' : $('input[name=companyAddress]').val(),
- 'city' : $('input[name=city]').val(),
- // 'state' : $('input[name=state]').val(),
- 'emailAddress' : $('input[name=emailAddress]').val(),
- 'message' : $('input[name=message]').val(),
- 'g-recaptcha' : $("#g-recaptcha-response").val(),
- };
- // process the form
- $.ajax({
- type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
- url : 'formMaster.php', // the url where we want to POST
- data : formData, // our data object
- dataType : 'json', // what type of data do we expect back from the server
- encode : true
- })
- // using the done promise callback
- .done(function(data) {
- // log data to the console so we can see
- console.log(data);
- // here we will handle errorsMSG and validation messages
- if ( ! data.success) {
- // handle errorsMSG for name ---------------
- if (data.errorsMSG.firstName) {
- $('#firstName-group').addClass('has-error'); // add the error class to show red input
- $('#firstName-group').append('<div class="help-block">' + data.errorsMSG.firstName + '</div>'); // add the actual error message under our input
- }
- // handle errorsMSG for name ---------------
- if (data.errorsMSG.lastName) {
- $('#lastName-group').addClass('has-error'); // add the error class to show red input
- $('#lastName-group').append('<div class="help-block">' + data.errorsMSG.lastName + '</div>'); // add the actual error message under our input
- }
- // handle errorsMSG for name ---------------
- if (data.errorsMSG.companyName) {
- $('#companyName-group').addClass('has-error'); // add the error class to show red input
- $('#companyName-group').append('<div class="help-block">' + data.errorsMSG.companyName + '</div>'); // add the actual error message under our input
- }
- // handle errorsMSG for Company Address ---------------
- if (data.errorsMSG.companyAddress) {
- $('#companyAddress-group').addClass('has-error'); // add the error class to show red input
- $('#companyAddress-group').append('<div class="help-block">' + data.errorsMSG.companyAddress + '</div>'); // add the actual error message under our input
- }
- // handle errorsMSG for Company Address ---------------
- if (data.errorsMSG.city) {
- $('#city-group').addClass('has-error'); // add the error class to show red input
- $('#city-group').append('<div class="help-block">' + data.errorsMSG.city + '</div>'); // add the actual error message under our input
- }
- // handle errorsMSG for Company Address ---------------
- // if (data.errorsMSG.state) {
- // $('#state-group').addClass('has-error'); // add the error class to show red input
- // $('#state-group').append('<div class="help-block">' + data.errorsMSG.state + '</div>'); // add the actual error message under our input
- // }
- // handle errorsMSG for Email Address ---------------
- if (data.errorsMSG.emailAddress) {
- $('#emailAddress-group').addClass('has-error'); // add the error class to show red input
- $('#emailAddress-group').append('<div class="help-block">' + data.errorsMSG.emailAddress + '</div>'); // add the actual error message under our input
- }
- // handle errorsMSG for superhero alias ---------------
- if (data.errorsMSG.message) {
- $('#message-group').addClass('has-error'); // add the error class to show red input
- $('#message-group').append('<div class="help-block">' + data.errorsMSG.message + '</div>'); // add the actual error message under our input
- }
- } else {
- // ALL GOOD! just show the success message!
- $('form').append('<div class="alert alert-success">' + data.message + '</div>');
- // usually after form submission, you'll want to redirect
- // window.location = '/thank-you'; // redirect a user to another page
- }
- })
- // using the fail promise callback
- .fail(function(data) {
- // show any errorsMSG
- // best to remove for production
- console.log(data);
- });
- // stop the form from submitting the normal way and refreshing the page
- event.preventDefault();
- });
- });
Add Comment
Please, Sign In to add comment