<?php
session_start(); 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}

	
	include("connect.php");

	$id=$_SESSION['id'];
	//$semail=$_SESSION['semail'];
	date_default_timezone_set("Asia/Kolkata");
	$mdate1= date('Y-m-d');
	$mdate2= date('Y-m-d H:i:s');
	$qmid=$_POST['qmid'];
	//$qnofordisplay=$_POST['qnofordisplay'];
	$email1=$_POST['email1'];
	$qpdf=$_POST['invoicepdf'];
	$esubject=$_POST['esubject'];
	$emessage=$_POST['emessage'];
	
	$bcc1=$_POST['bcc1'];

	
	$cc1=$_POST['cc1'];


	if(empty($_FILES['file']['name']) == false  )
	{
		$filename1 = date("YmdHis").str_replace(' ', '', $_FILES['file']['name']);
		$path = "eattachment";
		move_uploaded_file( $_FILES['file']['tmp_name'], $path ."/". $filename1 );
	}
	
	if(empty($_FILES['file1']['name']) == false  )
	{
		$filename2 = date("YmdHis").str_replace(' ', '', $_FILES['file1']['name']);
		$path = "eattachment";
		move_uploaded_file( $_FILES['file1']['tmp_name'], $path ."/". $filename2 );
	}
	
	if(empty($_FILES['file2']['name']) == false  )
	{
		$filename3 = date("YmdHis").str_replace(' ', '', $_FILES['file2']['name']);
		$path = "eattachment";
		move_uploaded_file( $_FILES['file2']['tmp_name'], $path ."/". $filename3 );
	}
	
	
	
	
		
			
			$eve1 = "select * from res_emailacc_master LIMIT 0,1";
			$re1 = mysqli_query($conn, $eve1);
			
			if(mysqli_num_rows($re1) > 0)
			{
				while($rt1 = mysqli_fetch_assoc($re1))
				{
					if($esubject=='')
					{	
						$subject=$emailsubject;
					}
					else
					{		
						$subject=$esubject;
						
					}
					
					$providername=$rt1['providername'];
					$email=$rt1['email'];
					$epassword=$rt1['epassword'];
					$esmtp=$rt1['esmtp'];
					$eport=$rt1['eport'];
					$essltls=$rt1['essltls'];
					
					
					$uu = "$email";
					$pp = "$epassword";
					
					ob_start();

					if($emessage=='')
					{	
						$newmessage="Hello sir/Mam ,<br><br>Thanks for being the part of Arth Technology family. Here we are sending you details have a look on that and let us know if you have any kind of query.<br><br>Thanks and regards<br>Arth Technology<br>http://arthtechnology.com<br>https://www.facebook.com/ArthTechnology"."<a><img width=\"1\" height=\"1\" src=\"http://arthtechno.tk/lacrm/t.php?i=".$last_id."\" style=\"border:none;\"></a>";;
					}
					else
					{
						$newmessage=nl2br($emessage);
					}

					require_once('class.phpmailer.php');
					
					$mail = new PHPMailer();
					$mail->IsSMTP();
					$mail->CharSet="UTF-8";
					$mail->SMTPSecure = "$essltls";
					$mail->Host = "$esmtp";
					$mail->Port = "$eport";
					$mail->Username = $uu;
					$mail->Password = $epassword;
					$mail->SMTPAuth = true;

					$mail->From = $uu;
					$mail->FromName = "$providername";
					
					if($email1=="")
					{
					}
					else
					{		
						$mail->AddAddress($email1);
					}

					
					///-------BCC-------
					if($bcc1=="")
					{
					}
					else
					{		
						$mail->AddBCC($bcc1);
					}

					
					
					
					///-------CC-------
					//$mail->AddCC($email);
					//$mail->AddCC($semail);
					if($cc1=="")
					{		
					}
					else
					{		
						$mail->AddCC($cc1);
					}

					
					//$mail->AddBCC($ebcc);
					//$mail->AddCC($ecc);
					
					$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
					$url1 = $_SERVER['REQUEST_URI']; //returns the current URL
					$parts = explode('/',$url1);
					$flen=count($parts);
					$dpath="/";
					for($i=1;$i<$flen-1;$i++)
					{
						$dpath=$dpath.$parts[$i]."/";
					}
					$fileurl = $protocol . $_SERVER['HTTP_HOST'] . $dpath;
					$attachmentUrl = $fileurl."download/".$qpdf;
					$qno1="Quotation ".$_SESSION['qno'];
					$tempFile = tempnam(sys_get_temp_dir(), 'attachment.pdf');  
					file_put_contents($tempFile, $attachmentUrl);
					//$mail->AddAttachment($tempFile);
					$mail->addStringAttachment(file_get_contents($attachmentUrl), $qpdf);

					if(empty($_FILES['file']['name']) == false && $filename1!='')
					{
							$attachmentUrl1 = $fileurl."eattachment/$filename1";
							$tempFile1 = tempnam(sys_get_temp_dir(), "$filename1");  
							file_put_contents($tempFile1, $attachmentUrl1);
							//$mail->AddAttachment($tempFile1);
							$mail->addStringAttachment(file_get_contents($attachmentUrl1), "$filename1");
							
						
					}
				
					$mail->IsHTML(true);
					$mail->Subject    = "$subject";
					$mail->AltBody    = ".";
					$mail->Body    = $newmessage.".";


					if(!$mail->Send())
					{
						
						$err=$mail->ErrorInfo;
						$eve_email = "INSERT INTO res_invoice_emaildetail (invoicemid,estatus,eremark,esentdatetime) VALUES('$qmid','Fail','$err','$mdate2')";
						$result_email= mysqli_query($conn, $eve_email);
						
					}
					else
					{		
						$eve_email = "INSERT INTO res_invoice_emaildetail (invoicemid,estatus,eremark,esentdatetime) VALUES('$qmid','Email Successfully sent','Email Successfully sent','$mdate2')";
						$result_email= mysqli_query($conn, $eve_email);
					}	
				}
			}
			
		

		$_SESSION['msg'] = "Reminder Addded";
			
	header("Location: invoicestatus.php?invoicemid=$qmid");


	
?>