📁 File Manager
Editing:
/home/u882412079/domains/birdbyteweb.com/public_html/process_form.php
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; // Set timezone to India date_default_timezone_set('Asia/Kolkata'); // Database connection $host = 'localhost'; $dbname = 'u882412079_birdbyteweb'; $username = 'u882412079_birdbyteweb'; $password = 'H8/8C0bl6yia'; try { // Establish a new database connection using PDO $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set error mode to exception } catch (PDOException $e) { // Display error message if connection fails die("<div class='alert alert-danger'>Connection failed: " . htmlspecialchars($e->getMessage()) . "</div>"); } // Capture form data with input sanitization $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); $mobile = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_NUMBER_INT); $msg = ""; $msg = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $current_time = date('Y-m-d H:i:s'); // Validate inputs if (empty($name) || empty($email) || empty($mobile) ) { // Check if any field is empty and display an error message echo '<div class="alert alert-danger">All fields are required.</div>'; exit; } if (!preg_match('/^[a-zA-Z\s]+$/', $name)) { // Validate name to contain only letters and spaces echo '<div class="alert alert-danger">Name must contain only letters and spaces.</div>'; exit; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // Validate email format and display an error message if invalid echo '<div class="alert alert-danger">Invalid email address.</div>'; exit; } if (!preg_match('/^[0-9]{10,18}$/', $mobile)) { // Validate mobile number format and display an error message if invalid echo '<div class="alert alert-danger">Invalid mobile number. Please enter a valid number between 10 to 15 digits.</div>'; exit; } // Save to database with prepared statements to prevent SQL injection try { // Prepare the SQL statement to insert form data into the database $stmt = $conn->prepare("INSERT INTO contact (name, email, mobile, msg, created_at) VALUES (:name, :email, :mobile, :msg, :created_at)"); // Bind parameters to prevent SQL injection $stmt->bindParam(':name', $name); $stmt->bindParam(':email', $email); $stmt->bindParam(':mobile', $mobile); $stmt->bindParam(':msg', $msg); $stmt->bindParam(':created_at', $current_time); // Execute the statement $stmt->execute(); } catch (Exception $e) { // Log the actual error message and display a generic error message to the user error_log($e->getMessage()); echo '<div class="alert alert-danger">Failed to save data. Please try again later.</div>'; exit; } // Send email using PHPMailer $mail = new PHPMailer(true); try { // Server settings for SMTP $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'birdbytewebsolution@gmail.com'; // Replace with your Gmail address $mail->Password = 'wqruhkrlxvozslbx'; // Replace with your Gmail App Password (do not use plain password) $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption $mail->Port = 587; // TCP port to connect to // Recipients $mail->setFrom('birdbytewebsolution@gmail.com', 'Birdbyte Web'); // Set the sender's email and name $mail->addAddress('birdbytewebsolution@gmail.com'); // Replace with the recipient's email address // Email content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Birdbyteweb.com Contact Form Submitted by '.$name; // Set email subject $mail->Body = "<strong>Name:</strong> " . htmlspecialchars($name) . "<br>" . "<strong>Email:</strong> " . htmlspecialchars($email) . "<br>" . "<strong>Mobile:</strong> " . htmlspecialchars($mobile) . "<br>" . "<strong>Message:</strong> " . nl2br(htmlspecialchars($msg)); // Set email body with sanitized input // Send the email $mail->send(); // Display success message if email is sent echo '<div class="alert alert-success">Thank you for contacting us. We will get back to you soon.</div>'; } catch (Exception $e) { // Display error message if email could not be sent echo '<div class="alert alert-danger">Message could not be sent. Please try again later.</div>'; }
Save
Cancel