<?php
function getTemplateByName($Name,$vars=array())
{
    $res= mysql_query("
                            SELECT * 
                            FROM mailtemplate 
                            WHERE Name='$Name'");
    $oRow = mysql_fetch_object($res);
	
    $allowed_vars = split(';',$oRow->Params);
	
    $body = $oRow->Body;

    foreach ($allowed_vars as $name) {
	
	
        if (!array_key_exists($name,$vars)) {
            $vars[$name] = '';
        }
        if (!empty($name)) $body = preg_replace("/\{$name\}/",$vars[$name],$body,-1);
    }
	
    return array ($oRow->Type,$body);
}

function send_mail  ($to_address,  $from_address,  $subject,  $message, $type, $on_switch, $send_type='outside',$charset='ISO 8859-1')  {


GLOBAL $Sess_UserId;
GLOBAL $Sess_UserName;

    if ($send_type == 'inside' || $send_type == 'inside_hidden'){

        $res = mysql_query("SELECT * FROM members WHERE mem_email='$to_address'");
        $receiver = mysql_fetch_assoc($res);

        $tempdate=date("Y/m/d");
        $message = mysql_escape_string($message); 
        $title = mysql_escape_string($title); 

        mysql_query("INSERT INTO messages SET 
                msg_senderid    = $Sess_UserId,
                msg_receiverid  = $receiver[mem_userid],
                msg_senderhandle= '$Sess_UserName',
                msg_title   = '$subject',
                msg_text    = '$message',
                msg_dateadded = '$tempdate',
                msg_read = 'U'".(($send_type == 'inside_hidden')?", msg_senderdel = 'Y'":""));
echo mysql_error();                

    }elseif($send_type == 'outside'){
            if ($type == "html" || $type == "text/html") {
                $headers = "MIME-Version: 1.0\n";
                $headers .= "Content-Type: text/html; charset=$charset\n";
                $headers .= "Content-Transfer-Encoding: 8bit\n";
                $headers .= "From: $from_address\n";
                $headers .= "Reply-To: $from_address\n";
            } else {
                $headers = "MIME-Version: 1.0\n";
                $headers = "Content-Type: text/plain; charset=$charset\n";
                $headers .= "From: $from_address\n";
                $headers .= "Reply-To: $from_address\n";
            }

            if ($on_switch=='ON')
            {
                mail($to_address,$subject,$message,$headers); 
            }
    }
}


?>
