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: 1213283 $
52 : * @package log4php
53 : * @subpackage appenders
54 : */
55 : class LoggerAppenderMail extends LoggerAppender {
56 :
57 : /** @var string 'from' field */
58 : protected $from = null;
59 :
60 : /** @var string 'subject' field */
61 : protected $subject = 'Log4php Report';
62 :
63 : /** @var string 'to' field */
64 : protected $to = null;
65 :
66 : /** @var indiciates if this appender should run in dry mode */
67 : protected $dry = false;
68 :
69 : /** @var string used to create mail body */
70 : protected $body = '';
71 :
72 : public function close() {
73 1 : if($this->closed != true) {
74 1 : $from = $this->from;
75 1 : $to = $this->to;
76 :
77 1 : if(!empty($this->body) and $from !== null and $to !== null and $this->layout !== null) {
78 1 : $subject = $this->subject;
79 1 : if(!$this->dry) {
80 0 : mail(
81 0 : $to, $subject,
82 0 : $this->layout->getHeader() . $this->body . $this->layout->getFooter(),
83 0 : "From: {$from}\r\n");
84 0 : } else {
85 1 : echo "DRY MODE OF MAIL APP.: Send mail to: ".$to." with content: ".$this->body;
86 : }
87 1 : }
88 1 : $this->closed = true;
89 1 : }
90 1 : }
91 :
92 : public function setSubject($subject) {
93 0 : $this->setString('subject', $subject);
94 0 : }
95 :
96 : public function setTo($to) {
97 1 : $this->setString('to', $to);
98 1 : }
99 :
100 : public function setFrom($from) {
101 1 : $this->setString('from', $from);
102 1 : }
103 :
104 : public function setDry($dry) {
105 1 : $this->setBoolean('dry', $dry);
106 1 : }
107 :
108 : public function append(LoggerLoggingEvent $event) {
109 1 : if($this->layout !== null) {
110 1 : $this->body .= $this->layout->format($event);
111 1 : }
112 1 : }
113 : }
|