<?php
/*
######################    CWB  PRO   #######################
############################################################
CWB PRO			$Name: release_version1_0_03112006 $
Revision		$Revision: 1.2 $
Author			$Author: zelteto $
Created 03/01/03        $Date: 2006/11/02 15:19:37 $
Writed by               GraFX (webmaster@grafxsoftware.com)
Scripts Home:           http://www.grafxsoftware.com
############################################################
File purpose            CLASS MAILSENDER
############################################################
*/
/*
// Getting the mail class started
// if the parameter is empty then text mail will be sent
// othervise html
$univMail= new UniversalMailSender("");
// setting up the to address
$univMail->setToAddress( $to_address, $to_name);
// setting up from address
$univMail->setFromAddress($from_address, $from_name);
// these functions are not implemented yet
$univMail->setCcAddress($cc_address, $cc_name);
$univMail->setBccAddress($bcc_address, $bcc_name);
// setting up subject
$univMail->setSubject($subject);
// setting up the charset
$univMail->setCharset($charset);
// setting up from text message if the type is text message
$univMail->setTextMessage($text_message="");
// setting up from text message if the type is html message
$univMail->setHtmlMessage($html_message="");
// setting up the type; this is for later settings
$univMail->setType($send_type="");
// setting up xmailer; not required  http://www.grafxsoftware.com/ is default
$univMail->setXmailer($xmailer="");
// setting up sender; not required  from_address is default
$univMail->setSender($sender="");
// setting up error_to; not required  from_address is default
$univMail->setErrorTo($error_to="");
// setting up debug; default is of but with 1 can be set up
$univMail->setDebug($debug);

// there are cases when the hosting company not allow mail headers as "From:" etc
// The default value is to put the headers but you can eliminate that by putting 0 as
// value for the $value argument Ex:$univMail->setHeaders(0)
$univMail->setHeaders($value)


// sending the mail; returns an error message
$univMail->SendMail();
// getting the debug info - only after SendMail
$univMail->getDebugMessage();
*/


class UniversalMailSender
{
 var $mail_to="";
 var $mail_from="";
 var $mail_to_addr="";
 var $mail_from_addr="";
 var $mail_cc="";
 var $mail_bcc="";
 var $mail_sender="";
 var $mail_error_to="";
 var $mail_reply_to="";
 var $mail_subject="";
 var $mail_xmailer="http://www.grafxsoftware.com/";
 var $mail_charset="UTF-8";
 var $mail_text_message;
 var $mail_html_message;
 var $mail_send_type;
 var $mail_debug="0";
 var $mail_debug_message="";
 var $security=1;
 var $headers=1;

function UniversalMailSender($send_type=""){
  if( empty($send_type) || $send_type==1)
   $this->mail_send_type=1;   // text
  else
   $this->mail_send_type=2;   // html
}
function setToAddress( $to_address, $to_name)
{
 $this->mail_to="$to_name <$to_address>";
 $this->mail_to_addr=$to_address;
}
function setFromAddress($from_address, $from_name)
{
$this->mail_from="$from_name <$from_address>";
$this->mail_from_addr=$from_address;
}
function setCcAddress($cc_address, $cc_name)
{
$this->mail_cc="$cc_name <$cc_address>";
}
function setBccAddress($bcc_address, $bcc_name)
{
$this->mail_bcc="$bcc_name <$bcc_address>";
}

function setSubject($subject)
{
$this->mail_subject=$subject;
}
function setCharset($charset)
{
$this->mail_charset=$charset;
}

function setTextMessage($text_message)
{
$this->mail_text_message=$text_message;
}
function setHtmlMessage($html_message)
{
$this->mail_html_message=$html_message;
}
function setType($send_type="")
{
  if( empty($send_type) || $send_type==1 )
   $this->mail_send_type=1;   // text
  else
   $this->mail_send_type=2;   // html
}
function setXmailer($xmailer="")
{
   $this->mail_xmailer=$xmailer;
}
function setSender($sender="")
{
   $this->mail_sender=$sender;
}
function setErrorTo($error_to="")
{
   $this->mail_error_to=$error_to;
}
function setReplyTo($reply_to="")
{
   $this->mail_reply_to=$reply_to;
}

function setHeaders($value)
{
   $this->headers=$value;
}


function setDebug($debug)
{
   $this->mail_debug=$debug;
}
function clean($text)
{
  return str_replace("\n"," ",str_replace("\r\n"," ",$text));
}
function getDebugMessage()
{
   return $this->mail_debug_message;
}

function securityFilter($tag_string){


 if( strpos($this->mail_from,'Content-Type:') === FALSE
 && strpos($this->mail_from,'Bcc:') === FALSE
 && strpos($this->mail_from,'bcc:') === FALSE
 && strpos($this->mail_from,'cc:') === FALSE
 )
 return false;
 else
   return true;

}

function SendMail(){
// security checkings

if($this->securityFilter($this->mail_from)
&& $this->securityFilter($this->mail_to_addr)
&& $this->securityFilter($this->mail_cc)
&& $this->securityFilter($this->mail_bcc)
&& $this->securityFilter($this->mail_to)
&& $this->securityFilter($this->mail_sender)
&& $this->securityFilter($this->mail_error_to)
&& $this->securityFilter($this->mail_reply_to)
)
return "";




if(defined("PHP_OS") && !strcmp(substr(PHP_OS,0,3),"WIN"))
    return $this->SendMailWindows($this->mail_to_addr,$this->mail_from,$this->mail_sender,$this->mail_error_to,$this->mail_reply_to,$this->mail_subject,$this->mail_send_type,$this->mail_text_message,$this->mail_html_message);
else
    return $this->SendMailLinux();
}

function SendMailWindows($to_address, $from_address,$mail_sender,$mail_error_to,$mail_reply_to, $subject,$send_type,$mail_text_message,$mail_html_message){
$headers ="";
if($this->headers)
{
	$headers .= "From: $from_address\r\n";
    if(empty($mail_reply_to))
	$headers .= "Reply-To: $from_address\r\n";
	else
	$headers .= "Reply-To: $mail_reply_to\r\n";

	if(empty($mail_sender))
	$headers .= "Sender: $from_address\r\n";
	else
	$headers .= "Sender: $mail_sender\r\n";

	if(empty($mail_error_to))
	$headers .= "Error-To: $from_address\r\n";
	else
	$headers .= "Error-To: $mail_error_to\r\n";
}
    $headers .= "X-Mailer: <$this->mail_xmailer>\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
	  if($send_type==1)
		{
		$headers  .= "Content-Type: text/plain; charset=\"".$this->mail_charset."\"\r\n";
		$message = $mail_text_message;
		}
	  else
		{
		$headers  .= "Content-Type: text/html; charset=\"".$this->mail_charset."\"\r\n";
        $message = $mail_html_message;
       }

 //  $extra_headers="-f $mail_from_addr";
  if($this->mail_debug==1)
    $this->mail_debug_message= $headers;

  return @mail($this->clean($to_address), $subject, $message, $headers, $extra_headers);
}

function SendMailLinux(){
// makeing the headers
   $break = "\n";

   $headers ="";
if($this->headers)
{
   $headers = "From: " . $this->clean($this->mail_from);
   $headers .= $break;

   if(empty($this->mail_reply_to))
   $headers .= "Reply-To:" . $this->clean($this->mail_from);
   else
   $headers .= "Reply-To:" . $this->clean($this->mail_reply_to);
   $headers .= $break;
   if(empty($this->mail_sender))
   $headers .= "Sender:" . $this->clean($this->mail_from);
   else
   $headers .= "Sender:" . $this->clean($this->mail_sender);
   $headers .= $break;

   $headers .= "Return-Path:" . $this->clean($this->mail_from);
   $headers .= $break;
   $headers .= "X-Mailer: " .  $this->mail_xmailer;
   $headers .= $break;

   if(empty($this->mail_error_to))
   $headers .= "Error-To:" . $this->clean($this->mail_from);
   else
   $headers .= "Error-To:" . $this->clean($this->mail_error_to);
   $headers .= $break;
   $headers  .="MIME-Version: 1.0 ";
   $headers  .= $break;
}
     if($send_type==1)
		{
		$headers  .= "Content-Type: text/plain; charset=\"".$this->mail_charset."\"\r\n";
		$message = $this->mail_text_message;
		}
	  else
		{
		$headers  .= "Content-Type: text/html; charset=\"".$this->mail_charset."\"\r\n";
        $message = $this->mail_html_message;
       }


   $headers  .= $break;

   //$extra_headers="-f $this->mail_from_addr";
  if($this->mail_debug==1)
    $this->mail_debug_message= $headers;

  return @mail($this->clean($this->mail_to_addr), $this->mail_subject, $message, $headers);
}
}
?>