Skip to content
This repository
tree: 66159a467c
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 138 lines (115 sloc) 3.493 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
<?php
/**
* PandraLoggerMail
*
* Mail implementation of PandraLogger. Handles all log messages crit to emergency
*
* @author Michael Pearson <pandra-support@phpgrease.net>
* @copyright 2010 phpgrease.net
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version 0.2
* @package pandra
*/
class PandraLoggerMail implements PandraLogger {

    private $_mailFrom = '';

    private $_mailTo = '';

    private $_subject = '';

    private $_maxPriority = PandraLog::LOG_CRIT;

    private $_sendDelay = FALSE;

    private $_headers = '';

    private $_messages = '';

    /**
*
* @param array $params 'from', 'to', 'subject', 'delay' (sends on destruct)
*/
    public function __construct(array $params) {
        if (isset($params['from'])) $this->_mailFrom = $params['from'];
        if (isset($params['to'])) $this->_mailFrom = $params['to'];
        if (isset($params['subject'])) $this->_mailFrom = $params['subject'];
        if (isset($params['delay'])) $this->_sendDelay = $params['delay'];
    }

    public function isOpen() {
        return function_exists('mail');
    }

    /**
* Mail shouldn't handle notices, info's etc.
* @param int $priority requested priority log
* @return boolean this logger will log for priority
*/
    public function isPriorityLogger($priority) {
        return ($priority <= $this->_maxPriority);
    }

    /**
* mailFrom mutator
* @param string $mailFrom set the email from address
*/
    public function setMailFrom($mailFrom) {
        $this->_mailFrom = $mailFrom;
    }

    /**
* mailFrom accessor
* @return string email from address
*/
    public function getMailFrom() {
        return $this->_mailFrom;
    }

    /**
* mailTo mutator
* @param string $mailTo set the email to address
*/
    public function setMailTo($mailTo) {
        $this->_mailTo = $mailTo;
    }

    /**
* mailTo accessor
* @return string email to address
*/
    public function getMailTo() {
        return $this->_mailTo;
    }


    /**
* subject mutator
* @param string $subject set the email subject line
*/
    public function setSubject($subject) {
        $this->_subject = $subject;
    }

    /**
* subject accessor
* @return string email subject line
*/
    public function getSubject() {
        return $this->_subject;
    }

    public function execute($priority, $message) {
        if ($this->isPriorityLogger($priority) && $this->_isOpen) {
            $this->_message .= $message;
            if (!$this->_sendDelay) {
                return $this->sendMail();
            }
            return TRUE;
        }
        return FALSE;
    }

    public function sendMail() {
        if (!empty($this->_messages)) {
            $this->subject = '['.strtoupper(PandraLog::$priorityStr[$priority]).'] '.$this->_subject;

            if (!empty($this->_mailFrom)) {
                $this->_headers = "From: ".$this->_mailFrom."\r\n" .
                        "Reply-To: ".$this->_mailFrom."\r\n";
            }
            $this->_headers .= "X-Mailer: PHP/".phpversion();
            return mail($this->_mailTo, $this->_subject, $this->messages, $this->_headers);
        }
        return FALSE;
    }

    public function close() {
        if ($this->_sendDelay) {
            $this->sendMail();
        }
    }

    public function __destruct() {
        $this->close();
    }
}
?>
Something went wrong with that request. Please try again.