<?php
ob_start();
ini_set("display_errors", "1");
error_reporting(E_ALL);

require_once 'PHPMailerAutoload.php';
include "../dbconfig.php";







if(isset($_POST['sent'])){
   // print_r($_POST);
    
    extract($_POST);
 
        $art = $art;
        $list = $list_name;
        $date = date("Y-m-d");
    $subject =$subject;

 $query = "select * from email_template where id = '$template_art' ";
 $result = mysqli_query($con,$query);
        $row = mysqli_fetch_assoc($result);
        
        $art = $row['template_name'];
       
   
   $array = array();
    
if(isset($name) && !empty($name)){
    
    
   foreach($name as $value2){
   
 
   $query ="insert into Campaign_lists values(null,'$value2','$list','$date')";
  $result = mysqli_query($con,$query);
  $array[] = $value2;
   }
 foreach($array as $email){

   
$sql="SELECT * FROM `social_link`";
$res=mysqli_query($con,$sql);
$row_link=mysqli_fetch_assoc($res);

$serv=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$serv=str_replace(".net", "", $serv);
$serv=str_replace(".com", "", $serv);

$message = $art;

$server =  str_replace("www.", "", $_SERVER['HTTP_HOST']);

$mail = new PHPMailer;
//$mail->isSMTP();
$mail->Host = 'mail.'.$server;
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;

$sql = "SELECT * FROM `smtp` WHERE email like '%info%'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($res);

    $mail->Username = $row['email'];
    $mail->Password = $row['password'];
    
$host=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$host=str_replace(".com", "", $host);
$host=str_replace(".net", "", $host);
$host=strtoupper($host);
$mail->setFrom($row['email'], $host);




echo $uemail = $email;
echo $name = $email;





$mail->addAddress($uemail,$name );

if ($mail->addReplyTo($row['email'])) {
    $mail->Subject = "$subject";
    //$mail->WordWrap = 50000;               // set word wrap
    $mail->Priority = 1; 
    $mail->IsHTML(true); 
    $mail->Body    = $art;
    $mail->AltBody = $art;
        if (!$mail->send()) {
          echo $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            header("location:../ip-admin/unactiveuser.php?msg=success");
          echo  $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        echo $msg = 'Invalid email address, message ignored.';
    }
}

}

}







if(isset($_POST['sent_all'])){

  
    extract($_POST);
 
 
 $subject =$subject;

 $query = "select * from email_template where id = '$template_art' ";
 $result = mysqli_query($con,$query);
        $row = mysqli_fetch_assoc($result);
        
        $art = $row['template_name'];
       
     
        $date = date("Y-m-d");
        if(isset($list_name) && !empty($list_name)){
         $list = $list_name;
        $query = "INSERT into list_name value(null,'$list','$date')";
        $reult = mysqli_query($con,$query);
        $list_id = mysqli_insert_id($con);
        
        }elseif(isset($list_old)){
           $list_id =  $list_old;
        }
        
    
if(isset($checkbox) && !empty($checkbox) && isset($art)){
    
   
   $id = explode(",",$checkbox);
   
   
   foreach($id as $value){
       $query = "select * from client_admin where id = $value";
       $result = mysqli_query($con,$query);
       
       $row  = mysqli_fetch_assoc($result);
       $arr[] = $row['email'];
     
   }
   
   foreach($arr as $uemail){
       
     $query = "Select * from Campaign_lists  where email = '$uemail' and list_id = $list_id";

    $res = mysqli_query($con,$query);
    $num_rows = mysqli_num_rows($res);
    
    if($num_rows > 0){
        
        
    }else{
        
      $query_insert_list = "insert into Campaign_lists values(null,'$uemail','$list_id','$date')";
       $result = mysqli_query($con,$query_insert_list);
      
    }
    
    $q = "select * from client_admin where email = '$uemail'";
      $result = mysqli_query($con,$q);
      $row = mysqli_fetch_assoc($result);
      $name = $row['first_name'];
       
$sql="SELECT * FROM `social_link`";
$res=mysqli_query($con,$sql);
$row_link=mysqli_fetch_assoc($res);

$serv=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$serv=str_replace(".net", "", $serv);
$serv=str_replace(".com", "", $serv);




$server =  str_replace("www.", "", $_SERVER['HTTP_HOST']);

$mail = new PHPMailer;
//$mail->isSMTP();
$mail->Host = 'mail.'.$server;
 $mail->AddReplyTo($server, 'Contact BLah' );
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;

$sql = "SELECT * FROM `smtp` WHERE email like '%info%'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($res);

    $mail->Username = $row['email'];
    $mail->Password = $row['password'];
    
$host=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$host=str_replace(".com", "", $host);
$host=str_replace(".net", "", $host);
$host=strtoupper($host);
$mail->setFrom($row['email'], $host);

$art = str_replace("first_name",$name,$art);

$mail->addAddress($uemail,$name );

if ($mail->addReplyTo($row['email'])) {
    $mail->Subject = "$subject";
    //$mail->WordWrap = 50000;               // set word wrap
    $mail->Priority = 1; 
    $mail->IsHTML(true); 
    $mail->Body    = $art;
    $mail->AltBody = $art;
        if (!$mail->send()) {
          echo $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
             header("location:../ip-admin/unactiveuser.php?msg1=success");
          echo  $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        echo $msg = 'Invalid email address, message ignored.';
    }
       
  }
}


}
die();
































if(isset($_POST['sent'])){
    
    extract($_POST);
    
    if(isset( $_POST['checkbox'])){
    
   $id = $_POST['checkbox'];
   foreach($id as $value){
       $query = "select * from client_admin where id = $value";
       $result = mysqli_query($con,$query);
     
       $row  = mysqli_fetch_assoc($result);
       $arr[] = $row['email'];
   }
   
   foreach($arr as $uemail){
       $q = "select * from client_admin where email = '$uemail'";
       $result = mysqli_query($con,$q);
       $row = mysqli_fetch_assoc($result);
       $name = $row['first_name'];
       
       
$sql="SELECT * FROM `social_link`";
$res=mysqli_query($con,$sql);
$row_link=mysqli_fetch_assoc($res);

$serv=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$serv=str_replace(".net", "", $serv);
$serv=str_replace(".com", "", $serv);

$q3 = "select * from email_template";
$result3 = mysqli_query($con,$q3);
$row3 = mysqli_fetch_assoc($result3);
$message = $row3['template_name'];

$server =  str_replace("www.", "", $_SERVER['HTTP_HOST']);

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.'.$server;
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;

$sql = "SELECT * FROM `smtp` WHERE email='contact@daviscuponline.com'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($res);

    $mail->Username = $row['email'];
    $mail->Password = $row['password'];
    
$host=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$host=str_replace(".com", "", $host);
$host=str_replace(".net", "", $host);
$host=strtoupper($host);
$mail->setFrom($row['email'], $host);





 




$mail->addAddress($uemail,$name );

if ($mail->addReplyTo($row['email'])) {
    $mail->Subject = "helloo $name";
    //$mail->WordWrap = 50000;               // set word wrap
    $mail->Priority = 1; 
    $mail->IsHTML(true); 
    $mail->Body    = $message;
    $mail->AltBody = $message;
        if (!$mail->send()) {
           echo $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
             header("location:../cpanel/unactiveuser.php?msg1=success");
          echo  $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        echo $msg = 'Invalid email address, message ignored.';
    }
       
   }
}

}


elseif(isset($single_Email)){

echo "working";
$sql="SELECT * FROM `social_link`";
$res=mysqli_query($con,$sql);
$row_link=mysqli_fetch_assoc($res);

$serv=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$serv=str_replace(".net", "", $serv);
$serv=str_replace(".com", "", $serv);

$message = $art;

$server =  str_replace("www.", "", $_SERVER['HTTP_HOST']);

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.'.$server;
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;

$sql = "SELECT * FROM `smtp` WHERE email='contact@daviscuponline.com'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($res);

    $mail->Username = $row['email'];
    $mail->Password = $row['password'];
    
$host=str_replace("www.", "", $_SERVER['HTTP_HOST']);
$host=str_replace(".com", "", $host);
$host=str_replace(".net", "", $host);
$host=strtoupper($host);
$mail->setFrom($row['email'], $host);


$id =$_GET['userart'];


 $query1 = "select * from client_admin where id = $id";
$result1 = mysqli_query($con,$query1);
$row2 = mysqli_fetch_assoc($result1);
echo $uemail = $row2['email'];
echo $name = $row2['first_name'];





$mail->addAddress($uemail,$name );

if ($mail->addReplyTo($row['email'])) {
    $mail->Subject = "$subject";
    //$mail->WordWrap = 50000;               // set word wrap
    $mail->Priority = 1; 
    $mail->IsHTML(true); 
    $mail->Body    = $art;
    $mail->AltBody = $art;
        if (!$mail->send()) {
           echo $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            header("location:../ip-admin/unactiveuser.php?msg=success");
          echo  $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        echo $msg = 'Invalid email address, message ignored.';
    }
}
    
?>