Location: PHPKode > projects > NmnLogger > NmnLogger1.1/drivers/mailLogger/NmnLoggerMailDriver.php
<?php 
    /*
    NmnLogger is a library that provides logging functionnality to php applications
    Copyright (C) 2006  Ivan Preziosi from netmeans.net - Rome.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    For more informations or to join the development of the library contact
    the author at: hide@address.com
    */

require_once "logger/drivers/NmnLoggerBaseDriver.php";
require_once "logger/drivers/mailLogger/phpMailer/class.phpmailer.php";

/**
 * This is the mail logger driver used to log message logs diretcly to a mail recipient.
 * As like every NmnLogger driver it must extend the NmnLoggerBaseDriver class.
 * 
 * @author  Ivan Preziosi <hide@address.com>
 * @version 1.1
 * @since   NmnLogger 0.5
 * @package NmnLogger
 *
 */
class NmnLoggerMailDriver extends NmnLoggerBaseDriver{
	// address separator for multiple recipient addresses
	const ADDRESS_SEPARATOR = ',';

	// phpmail properties
	private $mailObj = '';
	private $mailSenderName = '';
	private $mailSenderAddress = '';
	private $mailHost = '';
	private $phpmailMode = '';
	private $mailRecipients = '';
	private $mailSubject = '';
	private $mailUsername = '';
	private $mailPassword = '';

	public function __construct(){
		// default values
		$this->mailSenderName = 'NmnLogger Mail Driver';
		$this->mailSenderAddress = 'hide@address.com';
		$this->mailSenderHost = 'localhost';
		$this->mailSubject = 'NmnLogger: MailLogger Driver log message';
		$this->phpmailMode = 'smtp';
		$this->mailRecipients = 'hide@address.com'; // separate multiple names with a ','
		$this->mailUsername = '';
		$this->mailPassword = '';
		
		// instantiate object
		$this->mailObj = new phpmailer();
	}

	/**
	* Writes the message log down to the specified mail recipient(s).
	*/
	public function doLog(NmnMessageFactory $message){
		
		// error checking flag
		$recipientErrors = array();

		// set mail properties
		$this->mailObj->From = $this->mailSenderAddress;
		$this->mailObj->FromName = $this->mailSenderName;
		$this->mailObj->Host = $this->mailSenderHost;
		$this->mailObj->Subject = $this->mailSubject;
		$this->mailObj->Mailer = $this->phpmailMode;
		$this->mailObj->Body = $message->getFormattedMessage();
		
		// check if username and password provided, then if so set SMTPAuth to true and provide parameters for login
		if ($this->mailUsername != ''){
			$this->mailObj->SMTPAuth = true;
			$this->mailObj->Username = $this->mailUsername;
			$this->mailObj->Password = $this->mailPassword;
		}
		
		// create an array containing the recipients addresses
		$mailRecipients = explode(NmnLoggerMailDriver::ADDRESS_SEPARATOR,$this->mailRecipients);		
		
		foreach ($mailRecipients as $mailRecipient){
			// add recipient to the mail
			$this->mailObj->AddAddress($mailRecipient);

			if(!$this->mailObj->Send()){
				$recipientErrors[] = $this->mailObj->ErrorInfo;
			}
			// clear data for next loop
			$this->mailObj->ClearAddresses();
			
		}

		if (count($recipientErrors) > 0){
			$exceptionMessage = "NmnLogger - MailLogger driver: failed to send mail log: [".implode(",",$recipientErrors)."]";
			throw new Exception($exceptionMessage);
		}
	}

}



?>
Return current item: NmnLogger