1<?php 2/** 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * @package log4php 19 */ 20 21/** 22 * Appends log events to mail using php function {@link PHP_MANUAL#mail}. 23 * 24 * The appender sends all log events at once after the request has been 25 * finsished and the appender is beeing closed. 26 * 27 * Configurable parameters for this appender: 28 * 29 * - layout - Sets the layout class for this appender (required) 30 * - to - Sets the recipient of the mail (required) 31 * - from - Sets the sender of the mail (optional) 32 * - subject - Sets the subject of the mail (optional) 33 * 34 * An example: 35 * 36 * {@example ../../examples/php/appender_mail.php 19} 37 * 38 * {@example ../../examples/resources/appender_mail.properties 18} 39 * 40 * The above will output something like: 41 * <pre> 42 * Date: Tue, 8 Sep 2009 21:51:04 +0200 (CEST) 43 * From: someone@example.com 44 * To: root@localhost 45 * Subject: Log4php test 46 * 47 * Tue Sep 8 21:51:04 2009,120 [5485] FATAL root - Some critical message! 48 * Tue Sep 8 21:51:06 2009,120 [5485] FATAL root - Some more critical message! 49 * </pre> 50 51 * @version $Revision: 883108 $ 52 * @package log4php 53 * @subpackage appenders 54 */ 55class LoggerAppenderMail extends LoggerAppender { 56 57 /** @var string 'from' field */ 58 private $from = null; 59 60 /** @var string 'subject' field */ 61 private $subject = 'Log4php Report'; 62 63 /** @var string 'to' field */ 64 private $to = null; 65 66 /** @var indiciates if this appender should run in dry mode */ 67 private $dry = false; 68 69 /** @var string used to create mail body */ 70 private $body = ''; 71 72 /** 73 * Constructor. 74 * 75 * @param string $name appender name 76 */ 77 public function __construct($name = '') { 78 parent::__construct($name); 79 $this->requiresLayout = true; 80 } 81 82 public function __destruct() { 83 $this->close(); 84 } 85 86 public function activateOptions() { 87 $this->closed = false; 88 } 89 90 public function close() { 91 if($this->closed != true) { 92 $from = $this->from; 93 $to = $this->to; 94 95 if(!empty($this->body) and $from !== null and $to !== null and $this->layout !== null) { 96 $subject = $this->subject; 97 if(!$this->dry) { 98 mail( 99 $to, $subject, 100 $this->layout->getHeader() . $this->body . $this->layout->getFooter(), 101 "From: {$from}\r\n"); 102 } else { 103 echo "DRY MODE OF MAIL APP.: Send mail to: ".$to." with content: ".$this->body; 104 } 105 } 106 $this->closed = true; 107 } 108 } 109 110 public function setSubject($subject) { 111 $this->subject = $subject; 112 } 113 114 public function setTo($to) { 115 $this->to = $to; 116 } 117 118 public function setFrom($from) { 119 $this->from = $from; 120 } 121 122 public function setDry($dry) { 123 $this->dry = $dry; 124 } 125 126 public function append(LoggerLoggingEvent $event) { 127 if($this->layout !== null) { 128 $this->body .= $this->layout->format($event); 129 } 130 } 131} 132