|
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 |
|
19 /** |
|
20 * LoggerAppenderMailEvent appends individual log events via email. |
|
21 * |
|
22 * This appender is similar to LoggerAppenderMail, except that it sends each |
|
23 * each log event in an individual email message at the time when it occurs. |
|
24 * |
|
25 * This appender uses a layout. |
|
26 * |
|
27 * ## Configurable parameters: ## |
|
28 * |
|
29 * - **to** - Email address(es) to which the log will be sent. Multiple email |
|
30 * addresses may be specified by separating them with a comma. |
|
31 * - **from** - Email address which will be used in the From field. |
|
32 * - **subject** - Subject of the email message. |
|
33 * - **smtpHost** - Used to override the SMTP server. Only works on Windows. |
|
34 * - **port** - Used to override the default SMTP server port. Only works on |
|
35 * Windows. |
|
36 * |
|
37 * @version $Revision: 1343601 $ |
|
38 * @package log4php |
|
39 * @subpackage appenders |
|
40 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
|
41 * @link http://logging.apache.org/log4php/docs/appenders/mail-event.html Appender documentation |
|
42 */ |
|
43 class LoggerAppenderMailEvent extends LoggerAppender { |
|
44 |
|
45 /** |
|
46 * Email address to put in From field of the email. |
|
47 * @var string |
|
48 */ |
|
49 protected $from; |
|
50 |
|
51 /** |
|
52 * Mail server port (widnows only). |
|
53 * @var integer |
|
54 */ |
|
55 protected $port = 25; |
|
56 |
|
57 /** |
|
58 * Mail server hostname (windows only). |
|
59 * @var string |
|
60 */ |
|
61 protected $smtpHost; |
|
62 |
|
63 /** |
|
64 * The subject of the email. |
|
65 * @var string |
|
66 */ |
|
67 protected $subject = 'Log4php Report'; |
|
68 |
|
69 /** |
|
70 * One or more comma separated email addresses to which to send the email. |
|
71 * @var string |
|
72 */ |
|
73 protected $to = null; |
|
74 |
|
75 /** |
|
76 * Indiciates whether this appender should run in dry mode. |
|
77 * @deprecated |
|
78 * @var boolean |
|
79 */ |
|
80 protected $dry = false; |
|
81 |
|
82 public function activateOptions() { |
|
83 if (empty($this->to)) { |
|
84 $this->warn("Required parameter 'to' not set. Closing appender."); |
|
85 $this->close = true; |
|
86 return; |
|
87 } |
|
88 |
|
89 $sendmail_from = ini_get('sendmail_from'); |
|
90 if (empty($this->from) and empty($sendmail_from)) { |
|
91 $this->warn("Required parameter 'from' not set. Closing appender."); |
|
92 $this->close = true; |
|
93 return; |
|
94 } |
|
95 |
|
96 $this->closed = false; |
|
97 } |
|
98 |
|
99 public function append(LoggerLoggingEvent $event) { |
|
100 $smtpHost = $this->smtpHost; |
|
101 $prevSmtpHost = ini_get('SMTP'); |
|
102 if (!empty($smtpHost)) { |
|
103 ini_set('SMTP', $smtpHost); |
|
104 } |
|
105 |
|
106 $smtpPort = $this->port; |
|
107 $prevSmtpPort = ini_get('smtp_port'); |
|
108 if ($smtpPort > 0 and $smtpPort < 65535) { |
|
109 ini_set('smtp_port', $smtpPort); |
|
110 } |
|
111 |
|
112 // On unix only sendmail_path, which is PHP_INI_SYSTEM i.e. not changeable here, is used. |
|
113 |
|
114 $addHeader = empty($this->from) ? '' : "From: {$this->from}\r\n"; |
|
115 |
|
116 if (!$this->dry) { |
|
117 $result = mail($this->to, $this->subject, $this->layout->getHeader() . $this->layout->format($event) . $this->layout->getFooter($event), $addHeader); |
|
118 } else { |
|
119 echo "DRY MODE OF MAIL APP.: Send mail to: " . $this->to . " with additional headers '" . trim($addHeader) . "' and content: " . $this->layout->format($event); |
|
120 } |
|
121 |
|
122 ini_set('SMTP', $prevSmtpHost); |
|
123 ini_set('smtp_port', $prevSmtpPort); |
|
124 } |
|
125 |
|
126 /** Sets the 'from' parameter. */ |
|
127 public function setFrom($from) { |
|
128 $this->setString('from', $from); |
|
129 } |
|
130 |
|
131 /** Returns the 'from' parameter. */ |
|
132 public function getFrom() { |
|
133 return $this->from; |
|
134 } |
|
135 |
|
136 /** Sets the 'port' parameter. */ |
|
137 public function setPort($port) { |
|
138 $this->setPositiveInteger('port', $port); |
|
139 } |
|
140 |
|
141 /** Returns the 'port' parameter. */ |
|
142 public function getPort() { |
|
143 return $this->port; |
|
144 } |
|
145 |
|
146 /** Sets the 'smtpHost' parameter. */ |
|
147 public function setSmtpHost($smtpHost) { |
|
148 $this->setString('smtpHost', $smtpHost); |
|
149 } |
|
150 |
|
151 /** Returns the 'smtpHost' parameter. */ |
|
152 public function getSmtpHost() { |
|
153 return $this->smtpHost; |
|
154 } |
|
155 |
|
156 /** Sets the 'subject' parameter. */ |
|
157 public function setSubject($subject) { |
|
158 $this->setString('subject', $subject); |
|
159 } |
|
160 |
|
161 /** Returns the 'subject' parameter. */ |
|
162 public function getSubject() { |
|
163 return $this->subject; |
|
164 } |
|
165 |
|
166 /** Sets the 'to' parameter. */ |
|
167 public function setTo($to) { |
|
168 $this->setString('to', $to); |
|
169 } |
|
170 |
|
171 /** Returns the 'to' parameter. */ |
|
172 public function getTo() { |
|
173 return $this->to; |
|
174 } |
|
175 |
|
176 /** Enables or disables dry mode. */ |
|
177 public function setDry($dry) { |
|
178 $this->setBoolean('dry', $dry); |
|
179 } |
|
180 } |