<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require './vendor/autoload.php';

if (isset($_POST['vintage_watch'])) {
    $fisr_name = $_REQUEST['fname'];
    $last_name = $_REQUEST['lname'];
    $email = $_REQUEST['email'];
    $zip = $_REQUEST['zip'];
    $arae_code = $_REQUEST['arae_code'];
    $Phn_number = $_REQUEST['Phn_number'];
    $watch_details = $_REQUEST['watch_details'];
    $price = $_REQUEST['price'];
    $files = $_FILES['files'];
    
    $to1 = 'yuvika5588@gmail.com'; 
    $to2 = 'ashonb@rogers.com';
    $to3 = 'dromeo23@gmail.com';
    $from = 'ashonb@rogers.com'; 
    $fromName = 'Ashton Blakey'; 
    
    $validAttachments = array();
    $imagesAttachments = array();
    
    foreach($_FILES['files']['name'] as $index => $fileName) {
        $filePath = $_FILES['files']['tmp_name'][$index];
        $validAttachments[] = array($filePath, $fileName);              
    }    
    
    $targetDir = "uploads/"; 
    foreach($_FILES['files']['name'] as $key=>$val){ 
        $fileName = basename(time().'_'.$_FILES['files']['name'][$key]); 
        $targetFilePath = $targetDir . $fileName; 
        $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
        move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath);
        $imagesAttachments[] = array($targetFilePath, $fileName); 
    }
  
$htmlContent = ' 
<html>
   <head>
      <title></title>
   </head>
   <body>
      <center>
         <table cellspacing="5" cellpadding="5" style="max-width: 600px;width:100%;font-color:#888888;">
            <tbody>
               <tr>
                <td style="text-align: center; vertical-align: middle;">
                  <img src="https://wordpress.technoarray.com/ashton-blakey/assets/images/ashtonblakey_logo.png">
                </td>
               </tr>
               <tr>
                  <td style="padding:5px">
                     <table cellspacing="7" cellpadding="7" style="width:100%">
                        <tbody>
                           <tr style="font-color:#888888;">
                              <td>First Name: </td>
                              <td>'.$fisr_name.'</td>
                           </tr>
                           <tr style="font-color:#888888;">
                              <td>Last Name: </td>
                              <td>'.$last_name.'</td>
                           </tr>
                           <tr style="font-color:#888888;">
                              <td>Zip: </td>
                              <td>'.$zip.'</td>
                           </tr>
                           <tr style="font-color:#888888;">
                              <td>Email ID: </td>
                              <td>
                                 <a href="mailto:"'.$email.'" target="_blank">'.$email.'</a>
                              </td>
                           </tr>
                           <tr style="font-color:#888888;">
                              <td>Area Code: </td>
                              <td>'.$arae_code.'</td>
                           </tr>
                           <tr style="font-color:#888888;">
                              <td>Phone Number: </td>
                              <td>'.$Phn_number.'</td>
                           </tr>
                           <tr style="font-color:#888888;">
                              <td>Price: </td>
                              <td>'.$price.'</td>
                              </tr style="font-color:#888888;">
                           <tr>
                              <td>Watch Details: </td>
                              <td>'.$watch_details.'</td>
                           </tr>
                           <tr>
                              <td>Pictures: </td>
                              <td>&nbsp;</td>
                           </tr>
                           <tr>
                              <td colspan="2">
                              <table>';
                                    $i=0;
                                    $htmlContent .= '<tr>';
                                    foreach($imagesAttachments as $img) {
                                        if($i > 0 and $i % 2 == 0) {
                                            $htmlContent .='</tr><tr>';
                                        }
                                      $htmlContent .='<td><img src="https://wordpress.technoarray.com/ashton-blakey/'.$img[0].'" border="0" width="300px" height="auto" class="CToWUd a6T" data-bit="iit" tabindex="0"></td>'; 
                                    
                                    $i++;
                                    }
                                   $htmlContent .= '</tr>';
                                   $htmlContent .='</table></td>
                           </tr>
                           <tr>
                              <td colspan="2">Thanks.</td>
                           </tr>
                        </tbody>
                     </table>
                  </td>
               </tr>
            </tbody>
         </table>
      </center>
   </body>
</html>'; 
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer();
try {
    //Server settings
    $mail->SMTPDebug  = 2;
    $mail->Debugoutput = function($str, $level) {
        file_put_contents('phpmaile_smtp.log', gmdate('Y-m-d H:i:s'). "\t$level\t$str\n", FILE_APPEND | LOCK_EX);
    };
    $mail->isSMTP();
    //$mail->Host       = 'mail.ashton-blakey-antiques.com';
    $mail->Host       = 'mail.technoarray.com';
    $mail->SMTPAuth   = true;
    
    $mail->Username   = 'testing@technoarray.com';
    $mail->Password   = 'techno@123#';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port       = 465; 
    
    /*$mail->Username   = 'noreply@ashton-blakey-antiques.com';
    $mail->Password   = 'emaillist23886!';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port       = 465; */
    
    //Recipients
    $mail->setFrom('testing@technoarray.com', 'Michelle');
    $mail->addAddress($to1, $fromName); 
    $mail->addAddress($to2, $fromName);
    $mail->addAddress($to3, $fromName);
    $mail->addReplyTo($from, 'Information');
    /*$mail->addCC($to);
    $mail->addBCC($from);*/

    //Attachments
    /*foreach($validAttachments as $attachment) {
        $mail->AddAttachment($attachment[0], $attachment[1]);
    }*/
    
    //Content
    $mail->isHTML(true); 
    $mail->Subject = 'Sell Your Watch Form';
    $mail->Body    = $htmlContent;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        if($mail->send()){
            $response = array(
                "status" => '1',
                "message" => 'Mail has been sent',
            );
            echo json_encode($response);
        }
    } catch (Exception $e) {
        $response = array(
            "status" => '0',
            "message" => 'Something went wrong, go back and try again!',
        );
        echo json_encode($response);
    }
}
?>