<?php
 // Include the class:

require_once("constants.php");
require_once("db.class.php"); 

     
 class mailer
{
   function sendMail($to,$subject,$msg)
   {
       $configInfo= mailer::queryMailConfig();
        //In the following mysql_result code, zero indicates 1st row fetched from configTable. The settingValue of mailer is pulled form first row
        //The order of rows [settingName] is as follows:
        /*
                              mailer,
                              sendMailPath,
                              authentication,
                              SMTPUser,
                              SMTPPass,
                              SMTPHost,
                              mailFromName, 
                              mailFromEmail
        */
       $mailer= mysql_result($configInfo, 0,"settingValue");
       $auth= mysql_result($configInfo, 2,"settingValue"); 
       $SMTPUser= mysql_result($configInfo, 3,"settingValue");
       $SMTPPass= mysql_result($configInfo, 4,"settingValue");  
       $SMTPHost= mysql_result($configInfo, 5,"settingValue");
       $mailFromName= mysql_result($configInfo, 6,"settingValue"); 
       $mailFromEmail= mysql_result($configInfo, 7,"settingValue");  
     
        if(!get_magic_quotes_gpc()){
            $subject = addslashes($subject);}
           if(!get_magic_quotes_gpc()){
            $msg = addslashes($msg);}
            
       if($mailer=="SMTP")
       {
           return mailer::sendSMTPMail($to,$auth,$SMTPUser,$SMTPPass,$SMTPHost,$mailFromName,$mailFromEmail,$subject,$msg) ;
       }
       
       else if($mailer=="mail")
       {
           return mailer::sendPHPMail($to,$SMTPHost,$mailFromName,$mailFromEmail,$subject,$msg) ;
       }
   
   }
    function sendSMTPMail($to,$auth,$SMTPUser,$SMTPPass,$SMTPHost,$mailFromName,$mailFromEmail,$subject,$msg)
    {
    /* Purpose:
           - send mail relay (using MTA) with authentication via SSL conection (TLS encryption)
        */

        // manage errors
        error_reporting(0); // php errors  
        define('DISPLAY_XPM4_ERRORS', false); // display XPM4 errors
        // path to 'SMTP.php' file from XPM4 package
        require_once '../mailer/SMTP.php';
        // standard mail message RFC2822
     
        $m = 'From: '.$mailFromName.' <'.$mailFromEmail."> \r\n".
             'To: '.$to."\r\n".
             'Subject: '.$subject."\r\n".
             'Content-Type: text/plain'."\r\n\r\n".
             $msg;

        if($auth=="YES")
        {
        // connect to MTA server (relay) SMTPHost via SSL (TLS encryption) with authentication using port '465' and timeout '10' secounds
        // make sure you have OpenSSL module (extension) enable on your php configuration
            $c = SMTP::connect($SMTPHost, 465, $SMTPUser, $SMTPPass, 'tls', 10) or die("Failed to send SMTP email with Authentication. Please go back and check your email settings.");
        }
        else
        {
            $c = SMTP::connect($SMTPHost) or die("Failed to send SMTP email. Please go back and check your email settings.");
        
        }

        // send mail relay 
        $s = SMTP::send($c, array($to), $m, $mailFromEmail);

     
            if ($s) $return= true;
            else
            {
                print_r($_RESULT);
                $return= false;
            }

        // disconnect
        SMTP::disconnect($c);
        return $return;

    }
    function sendPHPMail($to,$SMTPHost,$mailFromName,$mailFromEmail,$subject,$msg)
    {
      /* Purpose:
       - set text message
       - send mail using local MTA support (function mail() from php)
       - print result
        */

        // manage errors
        error_reporting(0); // php errors
        //define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors

        // path to 'MAIL.php' file from XPM4 package
        require_once '../mailer/MAIL.php';

        // initialize MAIL class
        $m = new MAIL;
        // set from address
        $m->From($mailFromEmail);
        // add to address
        $m->AddTo($to);
        // set subject
        $m->Subject($subject);
        // set text message
        $m->Text($msg);

        // send mail
        if($m->Send() )
            return true;
        else
            return false;

    }
         //This function returns mail settings in an array format
     function queryMailConfig()
     {
        $db = new DB();
        $queryResult= $db->query("SELECT * FROM ConfigTable WHERE 
                                    settingName='mailer' or settingName='sendMailPath' or settingName='SMTPAuthentication' or
                                    settingName='SMTPUsername' or settingName='SMTPPassword' or settingName='SMTPHost' or
                                    settingName='mailFromName' or settingName='mailFromEmail'");
         
        return $queryResult;
     }
     
}
?>
