<?php include('../assets/constant/config.php'); 

try {

    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }

catch(PDOException $e) {

    echo "Connection failed: " . $e->getMessage();

}

?>
<!--  Author Name: Mayuri K. 
 for any PHP, Codeignitor or Laravel website or software development contact me at mayuri.infospace@gmail.com  -->
<?php
use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\SMTP;

use PHPMailer\PHPMailer\Exception;

require '../PHPMailer/src/Exception.php';

require '../PHPMailer/src/PHPMailer.php';

require '../PHPMailer/src/SMTP.php';

?>



<?php



// email



if (isset($_POST['submit'])) {
 // Retrieve form data
 $selectedMember = $_POST['to_member'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    // $date = $_POST['date'];

$member_id=$_POST['id'];
     // Query to get the email of the selected member
    $getEmailQuery = $conn->prepare("SELECT email FROM member WHERE name = :name AND delete_status = '0'");
    $getEmailQuery->bindParam(':name', $selectedMember);
    $getEmailQuery->execute();
    $row = $getEmailQuery->fetch(PDO::FETCH_ASSOC);
    $email = $row['email'];


$insertEmailQuery = $conn->prepare("INSERT INTO `send_email` (member_id,to_member, subject, message) VALUES (:member_id,:to_email, :subject, :message)");
    
 $insertEmailQuery->bindParam(':member_id', $member_id);
    $insertEmailQuery->bindParam(':to_email', $email);

    $insertEmailQuery->bindParam(':subject', $subject);
    $insertEmailQuery->bindParam(':message', $message);
    // $insertEmailQuery->bindParam(':date', $date);

$insertEmailQuery->execute();


            // print_r($stmt);

         // exit;



            // email start

            $stmt3 = $conn->prepare("SELECT * FROM emailsetting");

            $stmt3->execute();

            $result3 = $stmt3->fetchAll();

           

            foreach ($result3 as $row) {

                $smtp_server = $row['smtp_server'];

                $smtp_password = $row['smtp_password'];

                $smtp_enc = $row['smtp_type'];

                $smtp_username = $row['smtp_username'];

                $smtp_port = $row['stmp_port'];

                $email = $row['email'];

            }



            $dt = date('Y-m-d H:i:s');



            $msg1 = "Dear Admin, <br>

    You Have new message from <br> 

    Name: <strong>" . $selectedMember . "</strong>  <br>

    Email: <strong>" . $email . " </strong> <br>

    Subject: <strong>" .$subject . "</strong> <br>

    Message: <strong>" . $message . "</strong> <br>

     ";
          $mail = new PHPMailer(true);

            $mail->isSMTP();

            $mail->Host       = $smtp_server;

            $mail->SMTPAuth   = true;

            $mail->Username   = $smtp_username;

            $mail->Password   = $smtp_password;

            $mail->SMTPSecure = $smtp_enc;

            $mail->Port       = $smtp_port;



            $mail->setFrom($smtp_username);
             // $mail->addAddress($email);

            $mail->addAddress('');

             $mail->isHTML(true);

            $mail->Subject = 'Message For website on ' . $dt;

            $mail->Body    = $msg1;

            $mail->AltBody = $msg1;

         if ($mail->send())

            // email end

            {
      header("location: ../admin/manage_mail.php");

            }

        } else {

              header("location: ../send_email.php");

        }



