diff --git a/.env.example b/.env.example index 30a7c04..582a25b 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,13 @@ -RECAPTCHA_SECRET = "Your reCaptcha Secret Key" \ No newline at end of file +#This is an example of secret environmental variables +#original environmental variable should be created with .env filename + +DB_HOST = "Your Database Host Name Here" +DB_USER = "Your Database Username Here" +DB_PASS = "Your Database Password Here" +DB_NAME = "Your Database Name Here" +RECAPTCHA_SECRET = "Your reCaptcha Secret/Private Site Key Here" +SMTP_HOST = "Your SMTP Sender Host Name Here" +SMTP_USER = "Your SMTP Sender Username Here" +SMTP_PASS = "Your SMTP Sender Password Here" +SMTP_SENDTO = "Your Email Inbox Address to Receive Emails" +SMTP_PORT = 587 \ No newline at end of file diff --git a/htdocs/assets/js/contactform-config.js b/htdocs/assets/js/contactform-config.js index 110bee2..4fe629d 100644 --- a/htdocs/assets/js/contactform-config.js +++ b/htdocs/assets/js/contactform-config.js @@ -36,7 +36,7 @@ $(document).ready(function() { success: function(response) { if (response.alert && response.alertType) { - console.log(response.alert); + show_alert(response.alert, response.alertType); } }, error: function(jqXHR, textStatus, errorThrown) { @@ -44,7 +44,6 @@ $(document).ready(function() { }, complete: function() { $(sendBtn).html("SEND "); - console.log("completed"); } }); } diff --git a/htdocs/core/handle_contact.php b/htdocs/core/handle_contact.php index 27a8193..cba96a6 100644 --- a/htdocs/core/handle_contact.php +++ b/htdocs/core/handle_contact.php @@ -3,6 +3,10 @@ require 'vendor/autoload.php'; require '../../connection.php'; require 'query_functions.php'; +use PHPMailer\PHPMailer\PHPMailer; +use PHPMailer\PHPMailer\SMTP; +use PHPMailer\PHPMailer\Exception; + $dotenv = Dotenv\Dotenv::createImmutable('../../'); $dotenv->load(); @@ -14,6 +18,40 @@ function form_input_filter($conn, $data){ return $data; } +function send_alert($alertMessage, $alertType) { + echo json_encode(array("alert" => $alertMessage, "alertType" => $alertType)); +} + +function send_contact_email($name, $email, $message) { + require_once ("vendor/phpmailer/phpmailer/src/Exception.php"); + require_once ("vendor/phpmailer/phpmailer/src/PHPMailer.php"); + require_once ("vendor/phpmailer/phpmailer/src/SMTP.php"); + + $mail = new PHPMailer(true); + + try { + $mail->isSMTP(); + $mail->Host = $_ENV['SMTP_HOST']; + $mail->SMTPAuth = true; + $mail->Username = $_ENV['SMTP_USER']; + $mail->Password = $_ENV['SMTP_PASS']; + $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; + $mail->Port = $_ENV['SMTP_PORT']; + + $mail->setFrom($_ENV['SMTP_USER'], 'contact@neosubhamoy.com'); + $mail->addAddress($_ENV['SMTP_SENDTO']); + + $mail->isHTML(true); + $mail->Subject = "New Contact Form Submission by $name"; + $mail->Body = "name: $name
email: $email
message: $message
Quick Reply"; + + $mail->send(); + return true; + } catch (Exception $e) { + return false; + } +} + if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $jsonData = file_get_contents('php://input'); @@ -31,21 +69,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $responseData = json_decode($verifyResponse); if($responseData->success) { - + if(send_contact_email($name, $email, $message)) { + send_alert("Message Sent Successfully", "success"); + } + else { + send_alert("Something went wrong! Try again later", "danger"); + } } else { - + send_alert("Invalid reCaptcha! Please try again", "danger"); } } else { - + send_alert("Please check-in reCaptcha", "info"); } } else { - + send_alert("Please fill-up all fields", "info"); } - - echo json_encode(array("alert" => $alertMessage, "alertType" => $alertType)); } }