<?php 
date_default_timezone_set("Asia/kolkata");
include 'config.php';
require  "phpmailer/class.phpmailer.php"; //include phpmailer class By Deep Rana
//echo "adfhaslkdfjaklsjdflas";die;
//echo"<pre>";print_r($_SERVER);die;
    $tablesArray=array('tbl_sales_force','tbl_sales_force_medisafe');
    foreach($tablesArray as $table)
    {
        if($table=='tbl_sales_force')
        {
           $sql="SELECT * FROM $table WHERE email_flag='0'";
           if($result = $mysqli->query($sql))
           {
               while($row=$result->fetch_object())
               {
                      if($row->email!=""){
                        $subject="YTY New Product Inquiry - Sales";
                        //echo"<pre>";print_r($subject);die;
                          $bodyMessage='<p>Name: <strong>'.$row->first_name.'</strong><br>
                                        E-mail: <strong>'.$row->email.'</strong><br>
                                        Company Name: <strong>'.$row->company_name.'</strong><br>
                                        Contact Number: <strong>'.$row->contact_number.'</strong><br>
                                        City: <strong>'.$row->city.'</strong><br>
                                        State: <strong>'.$row->state.'</strong><br>
                                        Country: <strong>'.$row->country.'</strong><br>
                                        </p>
                                        <hr/>
                                        <p>
                                        I am interested in this category: <strong>'.$row->category.'</strong><br>
                                        I am interested in this polymer: <strong>'.$row->polymer.'</strong><br>
                                        I want one of your Specialty Products: <strong>'.$row->specialty_product.'</strong><br>
                                        </p>
                                        <hr/>
                                        <p>
                                        Weight: <strong>'.$row->weight.'</strong><br>
                                        Length: <strong>'.$row->length.'</strong><br>
                                        Texture: <strong>'.$row->texture.'</strong><br>
                                        Color: <strong>'.$row->color.'</strong><br>
                                        </p>
                                         <hr/>
                                        <p>
                                           If you are sourcing an existing product, can you send us a picture?:
                                        </p>
                                        <hr/>
                                        <p>
                                           Message:
                                            <strong>'.$row->remark.'</strong>
                                        </p>
                                        <hr/>
                                        <p>
                                           How did you find about us?: <strong>'.$row->did_you_find.'</strong>
                                        </p>
                                        <hr/>
                                        <p>
                                           What do you think about our site?: <strong>'.$row->think_about.'</strong>
                                        </p>' ;
                        $mail = new PHPMailer();  
                        // Set up SMTP  
                        $mail->IsSMTP();                // Sets up a SMTP connection  
                        $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
                        $mail->SMTPSecure = "ssl";      // Connect using a TLS connection  
                        $mail->Host = "email-smtp.us-east-1.amazonaws.com";  //Gmail SMTP server address-- iparable.co.in
                        $mail->Port = 465;  //Gmail SMTP port
                        // Authentication  
                        $mail->Username   = "AKIA4TM64HI4LPHXMK6L"; // Your full Gmail address -- smtp@iparable.co.in
                        $mail->Password   = "BFAufGeMYsb8sgH5Ta5VaTR/0omvCn6Ev3qFp+5wG83+"; // Your Gmail password-- NYz&cZgs{iU}
                        // Compose
                        $mail->SetFrom('inquiry@ytygroup.com.my','YTY Inquiries');
                        //$mail->AddReplyTo($toMailId, $toMailId);
                        $mail->Subject =$subject;      // Subject (which isn't  pawnesh.kumar@ytygroup.com.my,pawnesh.kumar@medisafe-tech.com required)  
                        $mail->MsgHTML($bodyMessage);
                        //**************Attachment**************/
                        if($row->ext!=""){ 
                           $path='/var/www/html/yty-sales-force/upload_files';
                           $file_name=$path."/".$row->sales_force_id.".".$row->ext; 
                           $view_name="contact-us.".$row->ext; 
                           $mail->addAttachment($file_name, $view_name);
                        }
                        $recipients = array(
                           'vikram.hora@gmail.com' => 'Vikram Hora',
                           'laura.chong@ytygroup.com.my' => 'Laura Chong',
                           'suresh.bhalla@ytygroup.com.my' => 'Suresh Bhalla',
                           'andrew.lanham@ytygroup.com.my' => 'Andrew Lanham'
                        );
                        foreach($recipients as $email => $name)
                        {
                           $mail->AddAddress($email); // Where to send it - Recipient--pawnesh.kumar@medisafe-tech.com
                        } 
                        //$mail->AddAddress('pawnesh.kumar@ytygroup.com.my'); // Where to send it - Recipient--pawnesh.kumar@medisafe-tech.com
                        $mail->AddBCC("pawnesh.kumar@ytygroup.com.my"); // Where to send it - Recipient
                       // echo"<pre>";print_r($mail);
                        $result = $mail->Send();		// Send!  
                        $message = $result ? '1' : '0';
                        if($message==1){
                          $updateDoTable="UPDATE $table SET email_flag='1' WHERE sales_force_id='".$row->sales_force_id."'";
                          $result = $mysqli->query($updateDoTable);
                          echo "sent successfully";
                        }
                        else
                            echo "Message Failed";
                            
                        unset($mail);
                    }
               }
           }
        }
        if($table=='tbl_sales_force_medisafe')
        {
           //echo  $table."<br>";
        }
    }
   //echo"<pre>";print_r($emailIdsArray);die;
    //****************************************Mail send end here by Deep Rana**************************************
    
?>