<?php

/*	b2b.class.mailer.php	*/

require_once('./classes/class.phpmailer.php');
require_once('./special/swift/swift_required.php'); 

class solarMailer extends PHPMailer {
// Set default variables for all new objects


public $From     	= "b2b.solarexplosives@gmail.com";
public $FromName 	= 'Admin@b2b.SolarExplosives';

public $Host		= "smtp.gmail.com";		// "smtp1.site.com;smtp2.site.com"; 
public $Port		= "465";
public $Mailer		= 'smtp';						// Alternative to IsSMTP()
public $Password	= 'Simple@3210';

public $WordWrap	= 0;
public $SMTPAuth	= true;
public $SMTPSecure	= 'ssl';

public $AddressBCC	= '';
public $AddressReplyTo	= '';

public function __construct($debug = 1){
  //  $this->Host = $this->Host;
    $this->Sender = $this->From;
   // $this->Username = $this->From;
//	$this->Password = $this->Password;
	
   // $this->SMTPAuth = $this->SMTPAuth;
    //$this->SMTPSecure = $this->SMTPSecure;
  //  $this->Port		 = $this->Port;
    
    $this->SMTPDebug = $debug;		// 0:No Debug Info, 1:Minimal Debug Info, 2:Verbose Debug Info
	$this->IsSMTP(false);
}

public function sendSolarMail($AddressTo, $Subject, $Body, $AddressCC=NULL, $AddressBCC=NULL, $IsHtml=False) {
/*	$success = FALSE;
	//return $success;

	if ($this->setAddress('AddAddress', $AddressTo)) {
		$this->setAddress('AddCC', $AddressCC);
		$this->setAddress('AddBCC', $AddressBCC);
		$this->SetFrom("b2b.solarexplosives@gmail.com","Admin b2b.SolarExplosives");
		$this->Subject = $Subject;
		$this->Body = $Body;
		//if($attachmentpath!=NULL){
		//$this->AddAttachment($attachmentpath);      // attachment
		//}
		try{
			$success = $this->Send();
			
		}catch(phpmailerException $e){ $mess = $e->errorMessage(); echo "<script> alert('$mess'); </script>"; }
		if (!$success) {
			$alertMsg = "$Subject e-mail is not sent." . '\n' . "EMailId: $AddressTo." . '\n' . "Mailer Error: $this->ErrorInfo";
			echo "<script> alert('$alertMsg'); </script>";
		}
	}
	return $success;
	*/
	$AddressToArr = explode(';',$AddressTo);
	$AddressCcArr = explode(';',$AddressCC);
	$AddressBccArr = explode(';',$AddressBCC);
	$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer  
	$message = Swift_Message::newInstance()  
			->setSubject($Subject) // Message subject  
			->setTo($AddressToArr) // Array of people to send to 
			->setFrom(array('b2b.solarexplosives@gmail.com' => 'Admin@B2B.SolarExplosives')); // From:  
			 if($AddressCC!=NULL || $AddressCC!='' ){
				$message->setCc($AddressCcArr);
			 }
			 if($AddressBCC!=NULL || $AddressBCC!='' ){
				$message->setBcc($AddressBccArr); 
			 }
			 if(!$IsHtml){
			  $message->setBody(nl2br($Body), 'text/html');// Attach that HTML message from earlier  
			  }
			  else{
				  $message->setBody($Body, 'text/html');// Attach that HTML message from earlier  
			  }
	$response = $mailer->send($message);
	return $response;
	
	
}

public function sendSolarMailPOApproval($AddressTo, $Subject, $Body, $AddressCC=NULL, $attachment) {
	$success = FALSE;
	//return $success;

	if ($this->setAddress('AddAddress', $AddressTo)) {
		$this->setAddress('AddCC', $AddressCC);

		$this->Subject = $Subject;
		$this->Body = $Body;
		if($attachment!=NULL){
			for($i=0; $i<sizeof($attachment); $i++){
				
			//	$p=$attachment[$i];
				//$path='/var/zpanel/hostdata/solarexp/public_html/b2b_solarexplosives_com/upload/re11PDF/PO/';
				//$path=$path.$p;
				$path=$attachment[$i];
				$this->AddAttachment($path);
			}
		}

		$success = $this->Send();
		if (!$success) {
			$alertMsg = "$Subject e-mail is not sent." . '\n' . "EMailId: $AddressTo." . '\n' . "Mailer Error: $this->ErrorInfo";
			echo "<script> alert('$alertMsg'); </script>";
		}
	}
	return $success;
}


protected function setAddress($AddressMethod, $AddressString) {
// $AddressString: ; delimited multiple To Addresses, each with Address followed by optional , delimited Name
// eg "dashgandhi@yahoo.com,Dasharath Gandhi;dashgandhi@gmail.com;dashgandhi@outlook.com,Dasharath"
	if (empty($AddressString)) return FALSE;

	$AddressArr = explode(';',$AddressString);
	foreach ($AddressArr as $AddressRow) {
		$AddressRowExplode = array_map('trim', explode(',', $AddressRow));
		$Address = $AddressRowExplode[0];
		$Name = (sizeof($AddressRowExplode)>1 ? $AddressRowExplode[1] : '');
		$this->{$AddressMethod}($Address, $Name);
	}
	return TRUE;
}

}


?>
