diff --git a/library/log4php/appenders/LoggerAppenderMailEvent.php b/library/log4php/appenders/LoggerAppenderMailEvent.php new file mode 100644 --- /dev/null +++ b/library/log4php/appenders/LoggerAppenderMailEvent.php @@ -0,0 +1,180 @@ +to)) { + $this->warn("Required parameter 'to' not set. Closing appender."); + $this->close = true; + return; + } + + $sendmail_from = ini_get('sendmail_from'); + if (empty($this->from) and empty($sendmail_from)) { + $this->warn("Required parameter 'from' not set. Closing appender."); + $this->close = true; + return; + } + + $this->closed = false; + } + + public function append(LoggerLoggingEvent $event) { + $smtpHost = $this->smtpHost; + $prevSmtpHost = ini_get('SMTP'); + if (!empty($smtpHost)) { + ini_set('SMTP', $smtpHost); + } + + $smtpPort = $this->port; + $prevSmtpPort = ini_get('smtp_port'); + if ($smtpPort > 0 and $smtpPort < 65535) { + ini_set('smtp_port', $smtpPort); + } + + // On unix only sendmail_path, which is PHP_INI_SYSTEM i.e. not changeable here, is used. + + $addHeader = empty($this->from) ? '' : "From: {$this->from}\r\n"; + + if (!$this->dry) { + $result = mail($this->to, $this->subject, $this->layout->getHeader() . $this->layout->format($event) . $this->layout->getFooter($event), $addHeader); + } else { + echo "DRY MODE OF MAIL APP.: Send mail to: " . $this->to . " with additional headers '" . trim($addHeader) . "' and content: " . $this->layout->format($event); + } + + ini_set('SMTP', $prevSmtpHost); + ini_set('smtp_port', $prevSmtpPort); + } + + /** Sets the 'from' parameter. */ + public function setFrom($from) { + $this->setString('from', $from); + } + + /** Returns the 'from' parameter. */ + public function getFrom() { + return $this->from; + } + + /** Sets the 'port' parameter. */ + public function setPort($port) { + $this->setPositiveInteger('port', $port); + } + + /** Returns the 'port' parameter. */ + public function getPort() { + return $this->port; + } + + /** Sets the 'smtpHost' parameter. */ + public function setSmtpHost($smtpHost) { + $this->setString('smtpHost', $smtpHost); + } + + /** Returns the 'smtpHost' parameter. */ + public function getSmtpHost() { + return $this->smtpHost; + } + + /** Sets the 'subject' parameter. */ + public function setSubject($subject) { + $this->setString('subject', $subject); + } + + /** Returns the 'subject' parameter. */ + public function getSubject() { + return $this->subject; + } + + /** Sets the 'to' parameter. */ + public function setTo($to) { + $this->setString('to', $to); + } + + /** Returns the 'to' parameter. */ + public function getTo() { + return $this->to; + } + + /** Enables or disables dry mode. */ + public function setDry($dry) { + $this->setBoolean('dry', $dry); + } +}