<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// captcha code
// scret key "6LdWhb8qAAAAACSv6cixKg-1lhci68anTsAQ6h4u"

    $recaptchaSecret = '6LdWhb8qAAAAACSv6cixKg-1lhci68anTsAQ6h4u'; // Use the Secret Key for reCAPTCHA v2
    $recaptchaResponse = $_POST['g-recaptcha-response']; // reCAPTCHA response from form
    $userIP = $_SERVER['REMOTE_ADDR']; // User's IP address

    // Send a POST request to Google to verify reCAPTCHA response
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$recaptchaSecret&response=$recaptchaResponse&remoteip=$userIP");
    $responseKeys = json_decode($response);

    // Check if reCAPTCHA was successfully validated
    if (intval($responseKeys->success) !== 1) {
        echo "Please complete the CAPTCHA verification.";
    } else {

//captch code


$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$product = $product ?? ($_POST['product'] ?? '');
$message  = $_POST['message'];

              $to = "info@manralsolar.com";
                $subject = "New Enquiry From Website";
                $body = "
                <!DOCTYPE html>
<html>
<head>
    <style>
        /* Add some basic styling for the email */
        body {
            font-family: Arial, sans-serif;
        }
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <h1>Website Enquiry</h1>
    <table>
        <tr>
            <th>Message</th>
        </tr>
        
    </table>
    <table>
        <tr>
            <th>Name</th>
            <th>Customer Email</th>
        </tr>
        <tr>
            <td>$name</td>
            <td>$email</td>
        </tr>
        <tr>
            <th>Phone No</th>
            <th>Product</th>
        </tr>
        <tr>
            <td>$phone</td>
            <td>$product</td>
        </tr>
        <tr>
            <th>Message</th>
        </tr>
        <tr>
            <td>$message</td>
        </tr>
       
    </table>";
    
    $body.="</table>
</body>
</html>
";
                $header = "MIME-Version: 1.0" . "\r\n";
                $header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                $header .= "From: info@manralsolar.com";
                


                
                if(mail($to,$subject,$body,$header))
                { 
                    echo "<script>window.location.href='thank_you.php';</script>";
    exit;
                }else{
                     echo "failed";
                }
}
// $smtpHost = 'smtp.gmail.com';
// $smtpPort = 587;
// $smtpUser = 'info@manralsolar.com';
// $smtpPass = 'Manral@2025';

// $from = 'info@manralsolar.com';
// $to = 'info@manralsolar.com';
// $subject = 'Website Enquiry Data';

// // Initialize an empty message
// $msg = "";


// if (isset($_POST['submit'])) {
//     $name = $_POST['name'];
//     $email = $_POST['email'];
//     $phone = $_POST['phone'];
//     $time = $_POST['product'];
//     $website = 'bhutani-133';

//       $output = "
//         <html>
//         <head>
//         <title> New Lead Alert </title>
//         </head>
//         <body>
//         <p>New Lead Alert</p>
//         <table border='1'>
//         <tr>
//         <th>Name</th>
//         <th>Email</th>
//         <th>Phone</th>
//         <th>Time</th>
//         <th>Website</th>
//         <th>Message</th>
//         </tr>
//         <tr>
//         <td>$name</td>
//         <td>$email</td>
//         <td>$phone</td>
//         <td>$time</td>
//         <td>$website</td>
//         </tr>
//         </table>
//         </body>
//         </html>
//         ";

//         $headers = "MIME-Version: 1.0" . "\r\n";
//         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
//         $headers .= "From: <info@manralsolar.com>" . "\r\n";
//         $headers .= "Cc: info@manralsolar.com" . "\r\n";

//         // Sending the email using SMTP
//         $smtpConnection = fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10);

//         if (!$smtpConnection) {
//             echo "Failed to connect to the SMTP server: $errstr ($errno)\n";
//             exit(1);
//         }

//         function get_smtp_response($smtpConnection) {
//             $response = '';
//             while ($str = fgets($smtpConnection, 515)) {
//                 $response .= $str;
//                 if (substr($str, 3, 1) == ' ') {
//                     break;
//                 }
//             }
//             return $response;
//         }

//         function send_smtp_command($smtpConnection, $command, $expectedResponse) {
//             fputs($smtpConnection, $command . "\r\n");
//             $response = get_smtp_response($smtpConnection);
//             if (substr($response, 0, 3) != $expectedResponse) {
//                 echo "Error: Unexpected SMTP response: $response\n";
//                 exit(1);
//             }
//         }

//         get_smtp_response($smtpConnection);
//         send_smtp_command($smtpConnection, 'EHLO ' . $smtpHost, '250');
//         send_smtp_command($smtpConnection, 'STARTTLS', '220');
//         stream_socket_enable_crypto($smtpConnection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
//         send_smtp_command($smtpConnection, 'EHLO ' . $smtpHost, '250');
//         send_smtp_command($smtpConnection, 'AUTH LOGIN', '334');
//         send_smtp_command($smtpConnection, base64_encode($smtpUser), '334');
//         send_smtp_command($smtpConnection, base64_encode($smtpPass), '235');
//         send_smtp_command($smtpConnection, 'MAIL FROM:<' . $from . '>', '250');
//         send_smtp_command($smtpConnection, 'RCPT TO:<' . $to . '>', '250');
//         send_smtp_command($smtpConnection, 'DATA', '354');
//         fputs($smtpConnection, "Subject: $subject\r\n");
//         fputs($smtpConnection, $headers . "\r\n");
//         fputs($smtpConnection, $output . "\r\n.\r\n");
//         get_smtp_response($smtpConnection);
//         send_smtp_command($smtpConnection, 'QUIT', '221');
//         fclose($smtpConnection);

//         echo "Email sent successfully!\n";
//         // $msg = "Data Successfully Submitted";
//         header("location: thank-you.php");
//     } else {
//         // $msg = "Something went wrong";
//                 echo "Email not send!\n";

//     }

?>