1
1
mirror of https://github.com/neosubhamoy/neosubhamoy-portfolio.git synced 2025-12-19 22:53:03 +05:30

(feat): added contact form submission notification via email

This commit is contained in:
2023-12-10 23:57:58 +05:30
parent cec8a42a52
commit 88839cd861
3 changed files with 61 additions and 9 deletions

View File

@@ -1 +1,13 @@
RECAPTCHA_SECRET = "Your reCaptcha Secret Key"
#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

View File

@@ -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 <i class='fa-regular fa-paper-plane ml-2'></i>");
console.log("completed");
}
});
}

View File

@@ -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 <br>email: $email <br>message: $message <br><a href='mailto:$email'>Quick Reply</a>";
$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));
}
}