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 * Log every events as a separate email.
23 *
24 * Configurable parameters for this appender are:
25 *
26 * - layout             - Sets the layout class for this appender (required)
27 * - to                 - Sets the recipient of the mail (required)
28 * - from               - Sets the sender of the mail (optional)
29 * - subject            - Sets the subject of the mail (optional)
30 * - smtpHost           - Sets the mail server (optional, default is ini_get('SMTP'))
31 * - port               - Sets the port of the mail server (optional, default is 25)
32 *
33 * An example:
34 *
35 * {@example ../../examples/php/appender_mailevent.php 19}
36 *
37 * {@example ../../examples/resources/appender_mailevent.properties 18}
38 *
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 * </pre>
49 *
50 * @version $Revision: 883108 $
51 * @package log4php
52 * @subpackage appenders
53 */
54class LoggerAppenderMailEvent extends LoggerAppender {
55
56	/**  'from' field (defaults to 'sendmail_from' from php.ini on win32).
57	 * @var string
58	 */
59	private $from = null;
60
61	/** Mailserver port (win32 only).
62	 * @var integer
63	 */
64	private $port = 25;
65
66	/** Mailserver hostname (win32 only).
67	 * @var string
68	 */
69	private $smtpHost = null;
70
71	/**
72	 * @var string 'subject' field
73	 */
74	private $subject = '';
75
76	/**
77	 * @var string 'to' field
78	 */
79	private $to = null;
80
81	/**
82	 * @access private
83	 */
84	protected $requiresLayout = true;
85
86	/** @var indiciates if this appender should run in dry mode */
87	private $dry = false;
88
89	/**
90	 * Constructor.
91	 *
92	 * @param string $name appender name
93	 */
94	public function __construct($name = '') {
95		parent::__construct($name);
96	}
97
98	public function __destruct() {
99       $this->close();
100   	}
101
102	public function activateOptions() {
103	    if (empty($this->layout)) {
104	        throw new LoggerException("LoggerAppenderMailEvent requires layout!");
105	    }
106	    if (empty($this->to)) {
107            throw new LoggerException("LoggerAppenderMailEvent was initialized with empty 'from' ($this->from) or 'to' ($this->to) Adress!");
108        }
109
110        $sendmail_from = ini_get('sendmail_from');
111        if (empty($this->from) and empty($sendmail_from)) {
112            throw new LoggerException("LoggerAppenderMailEvent requires 'from' or on win32 at least the ini variable sendmail_from!");
113        }
114
115        $this->closed = false;
116	}
117
118	public function close() {
119		$this->closed = true;
120	}
121
122	public function setFrom($from) {
123		$this->from = $from;
124	}
125
126	public function setPort($port) {
127		$this->port = (int)$port;
128	}
129
130	public function setSmtpHost($smtpHost) {
131		$this->smtpHost = $smtpHost;
132	}
133
134	public function setSubject($subject) {
135		$this->subject = $subject;
136	}
137
138	public function setTo($to) {
139		$this->to = $to;
140	}
141
142	public function setDry($dry) {
143		$this->dry = $dry;
144	}
145
146	public function append(LoggerLoggingEvent $event) {
147		$smtpHost = $this->smtpHost;
148		$prevSmtpHost = ini_get('SMTP');
149		if(!empty($smtpHost)) {
150			ini_set('SMTP', $smtpHost);
151		}
152
153		$smtpPort = $this->port;
154		$prevSmtpPort= ini_get('smtp_port');
155		if($smtpPort > 0 and $smtpPort < 65535) {
156			ini_set('smtp_port', $smtpPort);
157		}
158
159		// On unix only sendmail_path, which is PHP_INI_SYSTEM i.e. not changeable here, is used.
160
161		$addHeader = empty($this->from) ? '' : "From: {$this->from}\r\n";
162
163		if(!$this->dry) {
164			$result = mail($this->to, $this->subject,
165				$this->layout->getHeader() . $this->layout->format($event) . $this->layout->getFooter($event),
166				$addHeader);
167		    if ($result === false) {
168		        // The error message is only printed to stderr as warning. Any idea how to get it?
169		        throw new LoggerException("Error sending mail to '".$this->to."'!");
170		    }
171		} else {
172		    echo "DRY MODE OF MAIL APP.: Send mail to: ".$this->to." with additional headers '".trim($addHeader)."' and content: ".$this->layout->format($event);
173		}
174
175		ini_set('SMTP', $prevSmtpHost);
176		ini_set('smtp_port', $prevSmtpPort);
177	}
178}
179
180