 <?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK

require_once '../vendor/autoload.php';

include 'dbconnection.php';

$ID2 = $_GET["ID"]; 
$Email2 = $_GET["Email"]; 

$DB = mysqli_query($mysql,"SELECT dbname FROM tb_users WHERE email ='$Email2'");
while($row7 = mysqli_fetch_array($DB)){
$dbname = $row7 ['dbname'];}


$hostname2 = "localhost";
$username2 = "tempdpe5_nicolas";
$password2= "(Made7575)";
$conn = mysqli_connect($hostname2,$username2,$password2,$dbname);
if (mysqli_connect_errno()){
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
} 
mysqli_set_charset($conn ,"utf8");


$sql= mysqli_query($conn,"SELECT * FROM mailconfig");
$row = mysqli_fetch_assoc($sql);

 $Server= $row ['mailserver'];
 $Username = $row ['usuarioserver'];
 $Password = $row ['password']; 
 $UsuarioID = $row ['UsuarioID']; 
 $Url = $row ['sendcuota'];



$sql3 = mysqli_query($conn,"SELECT * FROM ordendetrabajo WHERE OrdenID ='$ID2'");
$count=mysqli_num_rows($sql3);
while($row3 = mysqli_fetch_array($sql3)){
   $OrdenID[] = $row3['OrdenID'];
   $Titulo[] = $row3['Titulo'];
   $Descripcion[] = $row3['Descripcion'];
   $Fechadeinicio[] = $row3['Fechadeinicio'];
   $Fechafinal[] = $row3['Fechafinal'];
   $Status[] = $row3['Status'];
   $Email[] = $row3['email'];
   $Comentarios[] = $row3['Comentarios'];
   $Condominio[] = $row3['Condominio'];
   }
   
for ($x=0; $x < $count; $x++) {


$sql4= mysqli_query($conn,"SELECT Email,Condominio FROM residentes WHERE Condominio ='$Condominio[$x]' ");
$count4=mysqli_num_rows($sql4);



$Emailcondo = array();
$Condominio3 = array();

while($row4 = mysqli_fetch_array($sql4)){
   $Condominio3[] = $row4['Condominio'];
   $Emailcondo[] = $row4['Email'];
   }
for ($i=0; $i < $count4; $i++){

try {
    // Create the SMTP Transport
    // Create the SMTP Transport
    $transport = (new Swift_SmtpTransport($Server,465,'ssl'))
        ->setUsername($Username)
        ->setPassword($Password);
 
    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);
 
 
   
    // Create a message
    $message = new Swift_Message();
 
    // Set a "subject"

     $message->setSubject('Orden de Trabajo '.$Status[$x].' | # '.$OrdenID[$x]);

    // Set the "From address"
    $message->setFrom([$Username => $UsuarioID]);
 
    // Set the "To address" [Use setTo method for multiple recipients, argument should be array]

    $message->setTo($Emailcondo[$i]);
    
    
    // Add "BCC" address [Use setBcc method for multiple recipients, argument should be array]
    //$message->addBcc('recipient@gmail.com', 'recipient name');
 
    //Add an "Attachment" (Also, the dynamic data can be attached)
    
  
    if ($Status[$x] =='Cancelada') {
    
  
     $message->setBody(
     
     '<html>' .
      ' <body>' .
      '<p> Saludos: </p>'.
      '<p> A continuación el detalle de la Orden de Trabajo. </p>'.
      '<table style="width:100%">'.
      '<tr>'.
      '<th>No. de Orden</th>'.
      '<th>Titulo</th>'. 
      '<th>Condominio</th>'. 
      '<th>Descripción</th>'.
      '<th>Estatus</th>'.
      '<th>Comentarios</th>'.
      '</tr>'.
      '<tr>'.
      '<td>'.$OrdenID[$x].'</td>'.
      '<td>'.$Titulo[$x].'</td>'.
      '<td>'.$Condominio[$x].'</td>'.
      '<td style="text-align: left;">'.$Descripcion[$x].'</td>'.
      '<td>'.$Status[$x].'</td>'.
      '<td style="text-align: left;">'.$Comentarios[$x].'</td>'.
      '</tr>'.
      '</table>'.
      '<style>'.
      'td, th {border: 1px solid #adadad;padding: 8px;text-align: center;}'.
      'th {padding-top: 5px;padding-bottom: 5px;background-color: #2c5285;color: white;font-weight:900;}'.

      '</style>'.
      ' </body>' .
      '</html>',
      'text/html'
       );
     
     } else {

     $message->setBody(
     
      '<html>' .
      ' <body>' .
      '<p> Saludos: </p>'.
      '<p> A continuación el detalle de la Orden de Trabajo. </p>'.
      '<table style="width:100%">'.
      '<tr>'.
      '<th>No. de Orden</th>'.
      '<th>Titulo</th>'. 
      '<th>Condominio</th>'. 
      '<th>Descripción</th>'.
      '<th>Inicio</th>'.
      '<th>Final</th>'.
      '<th>Estatus</th>'.
      '<th>Comentarios</th>'.
      '</tr>'.
      '<tr>'.
      '<td>'.$OrdenID[$x].'</td>'.
      '<td>'.$Titulo[$x].'</td>'.
      '<td>'.$Condominio[$x].'</td>'.
      '<td style="text-align: left;">'.$Descripcion[$x].'</td>'.
      '<td>'.$Fechadeinicio[$x].'</td>'.
      '<td>'.$Fechafinal[$x].'</td>'.
      '<td>'.$Status[$x].'</td>'.
      '<td style="text-align: left;">'.$Comentarios[$x].'</td>'.
      '</tr>'.
      '</table>'.
      '<style>'.
      'td, th {border: 1px solid #adadad;padding: 8px;text-align: center;}'.
      'th {padding-top: 5px;padding-bottom: 5px;background-color: #2c5285;color: white;font-weight:900;}'.

      '</style>'.
      ' </body>' .
      '</html>',
      'text/html'
       );

     }
 
    $result = $mailer->send($message);
   
  } 



catch (Exception $e) {
  echo $e->getMessage(); 
}

}
}
mysqli_close($conn);
mysqli_close($mysql);
?> 
