<?php
	
	ini_set('max_execution_time', 300); 
	require_once("sendgrid/vendor/autoload.php");
	require_once("sendgrid/lib/SendGrid.php");
	require_once(GLOBAL_INCLUDE_PATH.'include/PHPMailer/PHPMailerAutoload.php'); 
	//require_once('/home/chememi2/public_html/reach/include/PHPMailer/PHPMailerAutoload.php');
	// sparkpost api key
	//27c5eaa72800e26b561ae5a0524e07c6009a7dc9
	
 /********************************************* 
    * Publish On : Jan 10th, 2004                * 
    * Scripter   : Hermawan Haryanto             * 
    * Version    : 1.0                           * 
    * License    : GPL (General Public License)  * 
    **********************************************/ 
	/*
	function sendGridMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment="",$replyto="")
	{
		// sendgrid api key = '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'	 	
	 	$sendgrid = new SendGrid('rajivatre', 'Rsa551971');
		$email = new SendGrid\Email();
		$email  ->addTo($to_email)
				->setFrom($from_email)
				->setFromName($from_name)
				->setSubject($subject)
				->setText($text_message)
				->setHtml($html_message)
		;
		
		$email->addAttachment($attachment);
		//	$sendgrid->send($email);
		//	print_r($email);
			try {
    			$sendgrid->send($email);
				} catch(\SendGrid\Exception $e) {
    				echo $e->getCode();
    					foreach($e->getErrors() as $er) {
        					echo $er;
    			}
			}
			if($sendgrid)
			{
				return true;
			}
			else 
			{
				return false;
			}
		
	}
	*/
	
	
	function sendPHPMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment1="",$attachment2='',$attachment3='',$attachment4='',$attachment5='',$replyto="")
	{
							
			date_default_timezone_set('Etc/UTC');
			
			$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 = 'chemexcil.co.in';
			//Set the SMTP port number - likely to be 25, 465 or 587
			$mail->Port = 465;
			//Whether to use SMTP authentication
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = "ssl";
			//Username to use for SMTP authentication
			$mail->Username = 'noreply@chemexcil.co.in'; //$from_email;
			//Password to use for SMTP authentication
			$mail->Password = '@t2&ru@hFt~L';
			//Set who the message is to be sent from
			$mail-> setFrom($from_email,$from_name);
			//Set an alternative reply-to address
			if ($replyto!='')
			{
				$mail-> addReplyTo($replyto, $from_name);
			}
			else
			{			
				$mail-> addReplyTo($from_email, $from_name);
			}	
			//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');
			if ($attachment1!='')
            {
                $mail->addAttachment($attachment1);
            }
            if ($attachment2!='')
            {
                $mail->addAttachment($attachment2);
            }
            if ($attachment3!='')
            {
                $mail->addAttachment($attachment3);
            }
            if ($attachment4!='')
            {
                $mail->addAttachment($attachment4);
            }
            if ($attachment5!='')
            {
                $mail->addAttachment($attachment5);
            }
            //print_r($mail);
			//send the message, check for errors
			if (!$mail->send()) {
				//echo 'Mailer Error: ' . $mail->ErrorInfo;
				return false;
			} else {
				//echo 'Message sent!';
				return true;
			}
			unset($mail);
	}
	