<?php
	
	ini_set('max_execution_time', 600); 
	require_once("sendgrid/vendor/autoload.php");
	require_once("sendgrid/lib/SendGrid.php");
	
	// sparkpost api key
	//27c5eaa72800e26b561ae5a0524e07c6009a7dc9
	
 /********************************************* 
    * Publish On : Jan 10th, 2004                * 
    * Scripter   : Hermawan Haryanto             * 
    * Version    : 1.0                           * 
    * License    : GPL (General Public License)  * 
    **********************************************/ 

   /*function sendmail ($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment="",$replyto="")
    { 
        	$from = "$from_email"; 
        	$to   = "$to_email"; 
        	if ($replyto=="")
			{
				$replyto = $from_email;
			}
		
        	$boundary		= "--".md5(rand())."b"; 
        	$main_boundary	= "--".md5(rand())."a"; 
        
		$headers  = "From: $from_name <$from>\n"; 
      $headers .= "Reply-To: $from_name <$replyto> \n"; 
      $header  .= "Return-Path: $from_email <$from>\r\n"; 
		//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
		$headers .= 'Bcc: igrowsolutions@gmail.com' . "\r\n";
      $headers .= "Message-ID:<".$now." admin@".$_SERVER['SERVER_NAME'].">"."\r\n";
		$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
		$headers .= "MIME-Version: 1.0\n"; 
        
        $headers .= "Content-Type: multipart/related; boundary=$main_boundary\n"; 
        
        $message = "\n--$main_boundary\n"; 
        
        $message .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\n"; 
        $message .= "\n--$boundary\n"; 
        
        $message .= "Content-Type: text/plain; charset=windows-1252\n"; 
        $message .= "Content-Transfer-Encoding: quoted-printable\n\n"; 
        
        $message .= ($text_message!="")?"$text_message":"Text portion of HTML Email"; 
        $message .= "\n--$boundary\n";
        
        $message .= "Content-Type: text/html; charset=windows-1252\n"; 
        $message .= "Content-Transfer-Encoding: quoted-printable\n\n"; 
        
        $message .= str_replace ("=", "=3D", $html_message)."\n"; 
        
        if (isset ($attachment) && $attachment != "" && count ($attachment) >= 1) 
        { 
			$message .= "\n--$boundary--\n"; 
            for ($i=0; $i<count ($attachment); $i++) 
            { 
                $attfile = $attachment[$i]; 
					 if (is_file($attfile))
					 {  	                
                	$file_name = basename ($attfile); 
                	if ($fp = fopen ($attfile, "r"))
                	{ 
                		$fcontent = ""; 
                		while (!feof ($fp)) 
                		{ 
                    		$fcontent .= fgets ($fp, 1024); 
                		} 
                		$fcontent = chunk_split (base64_encode($fcontent)); 
                		@fclose ($fp);
                	}
                	$message .= "\n--$main_boundary\n"; 
                	$message .= "Content-Type: application/octetstream;\n"; 
                	$message .= "Content-Transfer-Encoding: base64\n"; 
                	$message .= "Content-Disposition: inline; filename=\"$file_name\"\n"; 
                	$message .= "Content-ID: <$file_name>\n\n";
                	$message .= $fcontent;
                }	
            } 
        }
        $message .= "\n--$main_boundary--\n"; 
        
        //$result= mail($to,$subject, $message, $headers);
		// following option need to check while sending mails
			$result= mail($to,$subject, $message, $headers);
        if ($result){
			return 1;
		}
		else
		{
			echo "Enquiry not Send.";
		}
    }
	*/
	function sendmailWOBCC ($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment="",$replyto="")
   { 
        $from = "$from_email"; 
        $to   = "$to_email"; 
        if ($replyto=="")
		{
			$replyto = $from_email;
		}
		
        $boundary		= "--".md5(rand())."b"; 
        $main_boundary	= "--".md5(rand())."a"; 
        
		$headers  = "From: $from_name <$from>\n"; 
        $headers .= "Reply-To: $from_name <$replyto> \n"; 
        $header  .= "Return-Path: $from_email <$from>\r\n"; 
		//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
		//$headers .= 'Bcc: igrowsolutions@gmail.com' . "\r\n";
        $headers .= "Message-ID:<".$now." admin@".$_SERVER['SERVER_NAME'].">"."\r\n";
		$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
		$headers .= "MIME-Version: 1.0\n"; 
        
        $headers .= "Content-Type: multipart/related; boundary=$main_boundary\n"; 
        
        $message = "\n--$main_boundary\n"; 
        
        $message .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\n"; 
        $message .= "\n--$boundary\n"; 
        
        $message .= "Content-Type: text/plain; charset=windows-1252\n"; 
        $message .= "Content-Transfer-Encoding: quoted-printable\n\n"; 
        
        $message .= ($text_message!="")?"$text_message":"Text portion of HTML Email"; 
        $message .= "\n--$boundary\n";
        
        $message .= "Content-Type: text/html; charset=windows-1252\n"; 
        $message .= "Content-Transfer-Encoding: quoted-printable\n\n"; 
        
        $message .= str_replace ("=", "=3D", $html_message)."\n"; 
        
        if (isset ($attachment) && $attachment != "" && count ($attachment) >= 1) 
        { 
			$message .= "\n--$boundary--\n"; 
            for ($i=0; $i<count ($attachment); $i++) 
            { 
                $attfile = $attachment[$i]; 
					 if (is_file($attfile))
					 {  	                
                	$file_name = basename ($attfile); 
                	if ($fp = fopen ($attfile, "r"))
                	{ 
                		$fcontent = ""; 
                		while (!feof ($fp)) 
                		{ 
                    		$fcontent .= fgets ($fp, 1024); 
                		} 
                		$fcontent = chunk_split (base64_encode($fcontent)); 
                		@fclose ($fp);
                	}	
                	$message .= "\n--$main_boundary\n"; 
                	$message .= "Content-Type: application/octetstream;\n"; 
                	$message .= "Content-Transfer-Encoding: base64\n"; 
                	$message .= "Content-Disposition: inline; filename=\"$file_name\"\n"; 
                	$message .= "Content-ID: <$file_name>\n\n";
                	$message .= $fcontent;
                }	
            } 
        }
        $message .= "\n--$main_boundary--\n"; 
        
        //$result= mail($to,$subject, $message, $headers);
		// following option need to check while sending mails
		$result= mail($to,$subject, $message, $headers);
        if ($result){
			//echo "Ok";
		}
		else
		{
			echo "Enquiry not Send.";
		}
    }
	
	
	function sendPHPMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment1="", $attachment2="", $attachment3="", $attachment4="", $attachment5="", $replyto="")
	{
				//$to_email = str_replace(';',',',$to_email);				
				
				$emailsExploded = explode(",", $to_email);
				if(!empty($emailsExploded))
				{
    				foreach($emailsExploded as $emailAddress)
    				{
        				$result = sendGridMail($from_name,$from_email , $to_name, $emailAddress, $subject, $text_message, $html_message, $attachment1, $attachment2, $attachment3, $attachment4, $attachment5, $replyto);
    				}
				}				
				//echo $result.' to '.$to_email;				
				return;				
			
			/*			
			date_default_timezone_set('Etc/UTC');
			
			require_once(GLOBAL_INCLUDE_PATH.'scripts/PHPMailer/PHPMailerAutoload.php');
			
			$mail = new PHPMailer();
			//Tell PHPMailer to use SMTP
			$mail->isSMTP();
			$mail->SMTPDebug = 0;
			$mail->Timeout = 30;			
			//Ask for HTML-friendly debug output
			//$mail->Debugoutput = 'html';
			//Set the hostname of the mail server
			$mail-> Host = 'mail.gulfyp.com';
			//Set the SMTP port number - likely to be 25, 465 or 587
			$mail->Port = 25;
			//Whether to use SMTP authentication
			$mail->SMTPAuth = true;
			//Username to use for SMTP authentication
			$mail->Username = 'support@gulfyp.com'; //$from_email;
			//Password to use for SMTP authentication
			$mail->Password = 'hT63$H@7drb2';
			//Set who the message is to be sent from
			$mail-> setFrom('support@gulfyp.com', 'Gulf Yellow Pages Online');
			//Set an alternative reply-to address
			if ($replyto!='')
			{
				$mail-> addReplyTo($replyto, $from_name);
			}
			else
			{			
				$mail-> addReplyTo('support@gulfyp.com', 'Gulf Yellow Pages Online');
			}	
			//Set who the message is to be sent to
			
			$emailsExploded = explode(",", $to_email);

			//If the array isn't empty, loop through
			if(!empty($emailsExploded))
			{
    			foreach($emailsExploded as $emailAddress)
    			{
        			$mail->AddAddress(trim($emailAddress),$to_name);
    			}
			} 
			else
			{
    			//This should not be the case.
    			throw new Exception('No emails found!');
			}
			
			//$mail-> addAddress($to_email, $to_name);
			//Set the subject line
			$mail-> Subject = $subject;
			
			//Read an HTML message body from an external file, convert referenced images to embedded,
			//convert HTML into a basic plain-text alternative body
			//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
			$mail->isHTML(true);                                  // Set email format to HTML

			$mail->msgHTML($html_message); 
			
			//Replace the plain text body with one created manually
			$mail->AltBody = $text_message;
			
			 
			
			//Attach an image file
			//$mail->addAttachment('images/phpmailer_mini.png');
			
			//send the message, check for errors
			if (!$mail->send()) {
				echo 'Mailer Error: ' . $mail->ErrorInfo;
			} else {
				echo 'Message sent!';
			}
			unset($mail);
			*/
	}
	
	
	function sendGridMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment1="", $attachment2="", $attachment3="", $attachment4="", $attachment5="", $replyto="")
	{
		// sendgrid api key = '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'	 	
	 	$sendgrid = new SendGrid('rajivatre', 'Rsa551971');
		$email = new SendGrid\Email();
		$email ->addTo($to_email)
				->setFrom($from_email)
				->setReplyTo($replyto)
				->setFromName($from_name)
				->setSubject($subject)
				->setText($text_message)
				->setHtml($html_message);
				

		
		//print_r($email);
		//die();
		try {
 			$sendgrid->send($email);
			} catch(\SendGrid\Exception $e) {
 				echo $e->getCode();
 					foreach($e->getErrors() as $er) {
     					echo $er;
 			}
		}
		if($sendgrid)
		{
			$status = "mail sent";
		}
		else 
		{
			$status = "mail not sent";
		}
	    //echo $status;
		unset($sendgrid);
		unset($email);
	}