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);
- var_dump($_POST);
- //Load Required Components
- require_once 'src/recaptcha_autoload.php';
- require_once "functions.php";
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception;
- require 'src/Exception.php';
- require 'src/PHPMailer.php';
- require 'src/SMTP.php';
- function validate($formData)
- {
- // Initiate Arrays
- $errorsMSG = array(); // array to hold validation errors
- $data = array(); // array to pass back data k:not needed
- $firstName = $formData['firstName'] ?? '';
- $lastName = $formData['lastName'];
- $companyName = $formData['companyName'];
- $companyAddress = $formData['companyAddress'];
- $emailAddress = $formData['emailAddress'];
- $message = $formData['message'];
- $pname_exp = '/^[a-zA-Z0-9\_]{2,20}/';
- // Validate firstName
- if (empty($firstName)) {
- $errorsMSG['firstName'] = 'First Name is required.';
- }
- // Check RegEx for Personal Name
- if (!preg_match($pname_exp, $firstName)) {
- $errorsMSG['firstName'] = 'First Name is required.';
- }
- // Validate lastName
- if (empty($lastName)) {
- $errorsMSG['lastName'] = 'Last Name is required.';
- }
- // Check RegEx for Personal Name
- if (!preg_match($pname_exp, $lastName)) {
- $errorsMSG['firstName'] = 'Last Name is required.';
- }
- // Validate companyName
- if (empty($companyName)) {
- $errorsMSG['companyName'] = 'Company Name is required.';
- }
- // Validate companyAddress
- if (empty($companyAddress)) {
- $errorsMSG['companyAddress'] = 'Company Address is required.';
- }
- // Validate emailAddress
- if (empty($emailAddress)) {
- $errorsMSG['emailAddress'] = 'Email Address is required.';
- }
- // Check if emailAddress is a valid email address
- if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
- $errorsMSG['emailAddress'] = 'Email address is not valid.';
- }
- // Validate message
- if (empty($message)) {
- $errorsMSG['message'] = 'Message is required.';
- }
- $captcha = checkCaptcha($formData['g-recaptcha-response']);
- if(!$captcha['isSuccess']){
- $errorsMSG ['captcha'] = $captcha['errorCodes'];
- }
- return $errorsMSG;
- //Send Errors
- if ( ! empty($errorsMSG)) {
- // if there are items in our errors array, return those errors
- $data['success'] = false;
- $data['errors'] = $errorsMSG;
- }
- echo json_encode($data);
- }
- // [
- // 'isSuccess' => bool,
- // 'errorCodes' => k: actually IDK :)
- // ]
- function checkCaptcha($g_recaptcha_response)
- {
- $recaptcha_secret_key = 'SECRET_PASSWORD';
- // $recaptcha = new ReCaptchaReCaptcha($recaptcha_secret_key, new ReCaptchaRequestMethodCurlPost());
- $recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_secret_key);
- $resp = $recaptcha->verify($g_recaptcha_response, $_SERVER['REMOTE_ADDR']);
- return [
- 'isSuccess' => $resp->isSuccess(),
- 'errorCodes' => $resp->getErrorCodes(),
- ];
- }
- function sendMail($formData)
- {
- $mail = new PHPMailer(true); // Passing `true` enables exceptions
- // 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 = 'ssl'; // Enable TLS encryption, `ssl` also accepted
- $mail->Port = 465; // 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 />";
- // @todo: make the other rows the same way, i.e. $formData['key'];
- $body_message.= "First Name: " . $formData['firstName'] . "<br />";
- $body_message.= "Last Name: " . $formData['lastName'] . "<br />";
- $body_message.= "Company Name: " . $formData['companyName'] . "<br />";
- $body_message.= "Company Address: " . $formData['companyAddress'] . "<br />";
- $body_message.= "City: " . $formData['city'] . "<br />";
- //$body_message.= "State: " . $state . "<br />";
- $body_message.= "Sender email: " . $formData['emailAddress'] . "<br />";
- // $body_message.= "Sender Phone: " . $formData['phoneNumber'] . "<br />";
- $body_message.= "\n\n" . $formData['message'];
- $mail->Body = $body_message;
- $mail->send();
- }
- /////////////////////////////////////////////////
- // process
- //this will be our whole response (jsoned later)
- $response = [
- //we'll change these later, possibly:
- 'success' => false,
- 'errors' => [],
- 'message' => 'There has been an issue sending your message',//could be an "OK" error message as well, depends on the 'success' key.
- ];
- // Copy $_POST to $formData
- $formData = $_POST;
- // mock / change $formData if needed (for tests etc)
- // $formData['blah'] = 'bleh';
- //validate
- $errors = validate($formData);
- if(!empty($errors)){
- $response['success'] = false;
- $response['errors'] = $errors;
- }else {//it's ok
- //send it
- try{
- sendMail($formData);
- //if we are here, then everything theoretically went fine.
- $response['success'] = true;
- $response['message'] = 'OK';
- }
- catch(Exception $e) {
- // @todo - in javascript, in the if ( ! data.success) .... do something about the
- $response['success'] = false;
- $response['message'] = 'There has been an issue sending your message';
- }
- }
- echo json_encode($response);
- exit;
- ?>
Add Comment
Please, Sign In to add comment