<?php
/* Namespace alias. */
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/* If you installed PHPMailer without Composer do this instead: */
    
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

//You can do them in different sequences as below

$name=strip_tags(stripslashes($_POST['name']));
$email=stripslashes(strip_tags($_POST['email']));
$subject=stripslashes(strip_tags("(GNK Website Contact Form) ".$_POST['subject']));
$message=strip_tags(stripslashes($_POST['message']));



function valid_email($email) {
    return (!preg_match("/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ix", $email)) ? false : true;
}   

if(strlen($name)<2){
    echo 'Your name should have at least 2 characters';
    exit();
}
if(!valid_email($email)){
    echo 'Please enter a valid email address';
    exit();
}
if(strlen($subject)<2){
    echo 'Your message should have at least 2 characters';
    exit();
}
if(strlen($message)<5){
    echo 'Your message should have at least 5 characters';
    exit();
}

/* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */
    $mail = new PHPMailer(TRUE);
    
    /* Open the try/catch block. */
    try {
        $mail->isHTML(FALSE);

       /* Set the reply to email. */
       $mail->addReplyTo($email, $name);

       /* Set the mail sender. */
       // $mail->setFrom($email, $name);
       $mail->setFrom('info@gnklaw.co.ke', "GNK Law");
    
       /* Add a recipient. */
       $mail->addAddress('info@gnklaw.co.ke');
    
       /* Set the subject. */
       $mail->Subject = ($subject);
    
       /* Set the mail message body. */
       $mail->Body = ($message);
    
      /* SMTP parameters. */

       
       /* Tells PHPMailer to use SMTP. */
       $mail->isSMTP();
       
       /* SMTP server address. */
       $mail->Host = 'smtp.sendgrid.net';
    
       /* Use SMTP authentication. */
       $mail->SMTPAuth = TRUE;
       
       /* Set the encryption system. */
       $mail->SMTPSecure = 'ssl';
       
       /* SMTP authentication username. */
       $mail->Username = 'apikey';
       
       /* SMTP authentication password. */
       $mail->Password = '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';
       
       /* Set the SMTP port. */
       $mail->Port = 465;
       /* Disable some SSL checks. */
       $mail->SMTPOptions = array(
          'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
          )
       );
    
       /* Finally send the mail. */
       if($mail->Send()) {
           //$arrResult['response'] = 'success';
           
           echo "Message Sent Successfully!";
        } else {
        	//$arrResult['response'] = 'error';
        	echo "There was a problem sending the mail: " . $mail->ErrorInfo;
        	exit;
        }
       
    }
    catch (Exception $e)
    {
       /* PHPMailer exception. */
      echo $e->errorMessage();
    }
    catch (\Exception $e)
    {
       /* PHP exception (note the backslash to select the global namespace Exception class). */
      echo $e->getMessage();
    }
?>
