<?php

if(isset($_POST['selectedcheckbox']) && $_POST['selectedcheckbox']!=""){

          
        $emails  = $_POST['selectedcheckbox'];
         $subject =$_POST['mailsubject']; //"Send Bulk Email in PHP using PHPMailer with Ajax JQuery";
        $message =$_POST['smstext']; //"<br>Powered By Webs Codex <br><br>Web: <a href='https://www.webscodex.com/' target='_blank'>Webs Codex</a>";
$attachment=$_POST['attachment'];


// $from="bpatcsc@bpatcsc.org";
// $to = "$emails";
// $subject = "$subject";
// $txt = "$message";
// $headers = "From:" .$from . "\r\n" .
// "CC: hasanpatc@gmail.com";

// //mail($to,$subject,$txt,$headers);

// if(mail($to,$subject,$txt,$headers)){
//     echo'<div class="alert alert-success alert-dismissible">
//             <button type="button" class="close" data-dismiss="alert">&times;</button>
//             Email sent successfully
//         </div>';
//     exit;
// }else{
//     echo'<div class="alert alert-danger alert-dismissible"> 
//             <button type="button" class="close" data-dismiss="alert">&times;</button>
//             Email not sent to Please try again or type correct email!
//         </div>';
//     exit;           
// }







// }



// if($_POST['button'] && isset($_FILES['attachment'])) 
// { 
  
//     $mailto = "$emails";
//     $from_mail = "bpatcsc@bpatcsc.org";
//     $replyto = "bpatcsc@bpatcsc.org";
//     $from_name ="BPATCSC";
//     $message = $message;
//     $subject = $subject;

//     $filename = $_FILES["$attachment"]["name"];
//     //echo $filename;
//     $content = chunk_split(base64_encode(file_get_contents($_FILES["$attachment"]["tmp_name"])));

//     $uid = md5(uniqid(time()));
//     $name = basename($file);
//     $header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
//     $header .= "Reply-To: " . $replyto . "\r\n";

//     $header .= "MIME-Version: 1.0\r\n";
//     $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
//     $header .= "This is a multi-part message in MIME format.\r\n";
//     $header .= "--" . $uid . "\r\n";

// // You add html "Content-type: text/html; charset=utf-8\n" or for Text "Content-type:text/plain; charset=iso-8859-1\r\n" by I.khan
//     $header .= "Content-type:text/html; charset=utf-8\n";
//     $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

// // User Message you can add HTML if You Selected HTML content
//     $header .= "<div style='color: red'>" . $message . "</div>\r\n\r\n";

//     $header .= "--" . $uid . "\r\n";
//     $header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
//     $header .= "Content-Transfer-Encoding: base64\r\n";
//     $header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n"; // For Attachment
//     $header .= $content . "\r\n\r\n";
//     $header .= "--" . $uid . "--";
//     if (mail($mailto, $subject, "", $header)) {
//         echo "<script>alert('Success');</script>"; // or use booleans here
//     } else {
//         echo "<script>alert('Failed');</script>";
//     }






   
        $mailTo="$emails";
        $message=$message;
        $subject    = $subject;
        $fromMail   = "bpatcsc@bpatcsc.org";
        $fromName   = "BPATCSC";
        $replyTo    = "no-reply";
        $filePath   = $attachment;





        $LE  = "\r\n";
        $uid = md5(uniqid(time()));
        $withAttachment = ($filePath !== NULL && file_exists($filePath));
    
        if($withAttachment){
            $fileName   = basename($filePath);
            $fileSize   = filesize($filePath);
            $handle     = fopen($filePath, "r");
            $content    = fread($handle, $fileSize);
            fclose($handle);
            $content = chunk_split(base64_encode($content));
        }
    
        $header = "From: ".$fromName." <".$fromMail.">$LE";
        $header .= "Reply-To: ".$replyTo."$LE";
        $header .= "MIME-Version: 1.0$LE";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"$LE$LE";
        $header .= "This is a multi-part message in MIME format.$LE";
        $header .= "--".$uid."$LE";
        $header .= "Content-type:text/html; charset=UTF-8$LE";
        $header .= "Content-Transfer-Encoding: 7bit$LE$LE";
        $header .= $message."$LE$LE";
    
        if($withAttachment){
            $header .= "--".$uid."$LE";
            $header .= "Content-Type: application/octet-stream; name=\"".$fileName."\"$LE";
            $header .= "Content-Transfer-Encoding: base64$LE";
            $header .= "Content-Disposition: attachment; filename=\"".$fileName."\"$LE$LE";
            $header .= $content."$LE$LE";
            $header .= "--".$uid."--";
        }
         //mail($mailTo, $subject, "", $header);
         if (mail($mailTo, $subject, "", $header)) {
                    echo "<script>alert('Success');</script>"; // or use booleans here
                } else {
                    echo "<script>alert('Failed');</script>";
                }


















} 

?>