<?php  $_REQUEST['src']='ext'; require_once(__DIR__ . '/../../functions/global.php');
require_once __DIR__ . '/../../functions/logging.php';
define("ROOT",'http://www.daso-id.com/enrep/');
require_once('Emogrifier.php');
function createHtmlBody($body){
$emogrifier = new \Pelago\Emogrifier();
$css = '';
$err = array();
$ss = file_get_contents('style.css');
if (false === $ss) {
$err[] = $stylesheet;
}
else {$css .= $ss;}
if (count($err)) {throw new sfException('Cannot find following stylesheets: %s', implode(', ', $err));}
$emogrifier->setHtml($body);
$emogrifier->setCss($css);
$mergedHtml = $emogrifier->emogrify();
return $mergedHtml;
};

function storeImage($n){
$f = $_FILES[$n];
$filename = $f["name"];
move_uploaded_file($_FILES[$n]["tmp_name"],  "../storage/".$filename);
return($filename);
}

echo "email:".$_REQUEST['recipient_1'];
$header=file_get_contents('email_template.php');
$header=str_replace('[MESSAGE]',$_REQUEST['message'],$header);
$header=str_replace('[SITE]',ROOT,$header);
$header=str_replace('[LOGO]',ROOT.'images/company/logo.png?'.rand(),$header);
$header=str_replace('[ENREP]',ROOT.'images/logo_border.png?'.rand(),$header);
$header=str_replace('[ADDRESS]',getCompanyDetails('address'),$header);
$header=str_replace('[CONTACT]',getCompanyDetails('contact'),$header);

include("../packages/flourish-classes-0.9.0/fException.php");
include("../packages/flourish-classes-0.9.0/fExpectedException.php");
include("../packages/flourish-classes-0.9.0/fValidationException.php");
include("../packages/flourish-classes-0.9.0/fCore.php");
include("../packages/flourish-classes-0.9.0/fUnexpectedException.php");
include("../packages/flourish-classes-0.9.0/fProgrammerException.php");

include("../packages/flourish-classes-0.9.0/fEmail.php");
include("../packages/flourish-classes-0.9.0/fMailbox.php");
include("../packages/flourish-classes-0.9.0/fCryptography.php"); 
include("../packages/flourish-classes-0.9.0/fFile.php");
include("../packages/flourish-classes-0.9.0/fFilesystem.php"); 

$email = new fEmail();
$email->addRecipient($_REQUEST['recipient_1']);$rstring="Sent Email to ".$_REQUEST['recipient_1'];
if(!empty($_REQUEST['recipient_2'])){$email->addBCCRecipient($_REQUEST['recipient_2']); $rstring.=",".$_REQUEST['recipient_2'];}
if(!empty($_REQUEST['recipient_3'])){$email->addBCCRecipient($_REQUEST['recipient_3']); $rstring.=",".$_REQUEST['recipient_3'];}
if(!empty($_REQUEST['recipient_4'])){$email->addBCCRecipient($_REQUEST['recipient_4']); $rstring.=",".$_REQUEST['recipient_4'];}
if(!empty($_REQUEST['recipient_5'])){$email->addBCCRecipient($_REQUEST['recipient_5']); $rstring.=",".$_REQUEST['recipient_5'];}
$email->setFromEmail('ENREP Reporting system <system@enrep.co.uk>');
$email->setSubject($_REQUEST['subject']);
$email->setHTMLBody(createHtmlBody($header));
$email->setBody(createHtmlBody($header));

if(!empty($_REQUEST['report_1'])){
$email->addAttachment(new fFile('../'.$_REQUEST['source_1'].'/'.$_REQUEST['report_1']));
$attach="|attached 1";
$fstring="with the file(s) ".$_REQUEST['report_1'];
}
if(!empty($_REQUEST['file_1'])){
$email->addAttachment(new fFile('../'.$_REQUEST['source_1'].'/'.$_REQUEST['file_1']));
$attach="|attached 1";
$fstring="with the file(s) ".$_REQUEST['file_1'];
}
if(!empty($_FILES['attachment_1']['name'])){$file1=storeImage('attachment_1');
$email->addAttachment(new fFile('../storage/'.$file1));	$attach.="|attached 1"; $fstring="with the file(s) ".$_REQUEST['file_1'];
}
if(!empty($_FILES['attachment_2']['name'])){$file2=storeImage('attachment_2');
$email->addAttachment(new fFile('../storage/'.$file2));	$attach.="|attached 2"; $fstring.=",".$file2;
}
if(!empty($_FILES['attachment_3']['name'])){$file3=storeImage('attachment_3');
$email->addAttachment(new fFile('../storage/'.$file3));	$attach.="|attached 3"; $fstring.=",".$file3;
}
if(!empty($_FILES['attachment_4']['name'])){$file4=storeImage('attachment_4');
$email->addAttachment(new fFile('../storage/'.$file4));	$attach.="|attached 4"; $fstring.=",".$file4;
}
$message_id = $email->send();
enrep_log_email('sent', (string)($_REQUEST['subject'] ?? ''), [$rstring], [$fstring], 'Sent legacy email');

//HSE NOTIFICATION

if(!empty($_REQUEST['hse'])){
$header=file_get_contents('../includes/master/email_template.php');
$header=str_replace('[MESSAGE]',"Please find attached the details of the defects at ".$_REQUEST['company'],$header);
$header=str_replace('[LOGO]','http://www.daso-id.com/enrep/images/company/logo.png?'.rand(),$header);
$header=str_replace('[ENREP]','http://www.daso-id.com/enrep/images/logo_border.png?'.rand(),$header);
$header=str_replace('[ADDRESS]',getCompanyDetails('address'),$header);
$header=str_replace('[CONTACT]',getCompanyDetails('contact'),$header);

	
$email = new fEmail();
$email->addRecipient($_REQUEST['hse']);$rstring="Sent Email to HSE (".$_REQUEST['hse'].")";
$email->setFromEmail('ENREP Reporting system <system@enrep.co.uk>');
$email->setSubject("Defect Notification");
$email->setHTMLBody($header);
$email->setBody("Please find attached the details of the defects at ".$_REQUEST['company']);


$email->addAttachment(new fFile('../'.$_REQUEST['source_1'].'/'.$_REQUEST['defectList']));
$attach="|attached 1";
$fstring="with the file(s) ".$_REQUEST['report_1'];
if(!empty($_FILES['attachment_2']['name'])){$email->addAttachment(new fFile('../storage/'.$file2));	$fstring.=",".$file2;}
if(!empty($_FILES['attachment_3']['name'])){$email->addAttachment(new fFile('../storage/'.$file3));	$fstring.=",".$file3;}
if(!empty($_FILES['attachment_4']['name'])){$email->addAttachment(new fFile('../storage/'.$file4));	$fstring.=",".$file4;}
$message_id = $email->send();	

enrep_log_email('sent', 'Defect Notification', [(string)($_REQUEST['hse'] ?? '')], [(string)($_REQUEST['defectList'] ?? '')], 'Sent legacy HSE defect notification');   
}




echo "done".$attach;
?>
