      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:  * LoggerAppenderMail appends log events via email.
     21:  *
     22:  * This appender does not send individual emails for each logging requests but
     23:  * will collect them in a buffer and send them all in a single email once the
     24:  * appender is closed (i.e. when the script exists). Because of this, it may
     25:  * not appropriate for long running scripts, in which case
     26:  * LoggerAppenderMailEvent might be a better choice.
     27:  *
     28:  * This appender uses a layout.
     29:  *
     30:  * ## Configurable parameters: ##
     31:  *
     32:  * - **to** - Email address(es) to which the log will be sent. Multiple email
     33:  *     addresses may be specified by separating them with a comma.
     34:  * - **from** - Email address which will be used in the From field.
     35:  * - **subject** - Subject of the email message.
     36:  *
     37:  * @version $Revision: 1337820 $
     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.html Appender documentation
     42:  */
     43: class LoggerAppenderMail extends LoggerAppender {
     44:
     45:     /**
     46:      * Email address to put in From field of the email.
     47:      * @var string
     48:      */
     49:     protected $from = null;
     50:
     51:     /**
     52:      * The subject of the email.
     53:      * @var string
     54:      */
     55:     protected $subject = 'Log4php Report';
     56:
     57:     /**
     58:      * One or more comma separated email addresses to which to send the email.
     59:      * @var string
     60:      */
     61:     protected $to = null;
     62:
     63:     /**
     64:      * Indiciates whether this appender should run in dry mode.
     65:      * @deprecated
     66:      * @var boolean
     67:      */
     68:     protected $dry = false;
     69:
     70:     /**
     71:      * Buffer which holds the email contents before it is sent.
     72:      * @var string
     73:      */
     74:     protected $body = '';
     75:
     76:     public function append(LoggerLoggingEvent $event) {
     77:         if($this->layout !== null) {
     78:             $this->body .= $this->layout->format($event);
     79:         }
     80:     }
     81:
     82:     public function close() {
     83:         if($this->closed != true) {
     84:             $from = $this->from;
     85:             $to = $this->to;
     86:
     87:             if(!empty($this->body) and $from !== null and $to !== null and $this->layout !== null) {
     88:                 $subject = $this->subject;
     89:                 if(!$this->dry) {
     90:                     mail(
     91:                         $to, $subject,
     92:                         $this->layout->getHeader() . $this->body . $this->layout->getFooter(),
     93:                         "From: {$from}\r\n");
     94:                 } else {
     95:                     echo "DRY MODE OF MAIL APP.: Send mail to: ".$to." with content: ".$this->body;
     96:                 }
     97:             }
     98:             $this->closed = true;
     99:         }
    100:     }
    101:
    102:     /** Sets the 'subject' parameter. */
    103:     public function setSubject($subject) {
    104:         $this->setString('subject', $subject);
    105:     }
    106:
    107:     /** Returns the 'subject' parameter. */
    108:     public function getSubject() {
    109:         return $this->subject;
    110:     }
    111:
    112:     /** Sets the 'to' parameter. */
    113:     public function setTo($to) {
    114:         $this->setString('to', $to);
    115:     }
    116:
    117:     /** Returns the 'to' parameter. */
    118:     public function getTo() {
    119:         return $this->to;
    120:     }
    121:
    122:     /** Sets the 'from' parameter. */
    123:     public function setFrom($from) {
    124:         $this->setString('from', $from);
    125:     }
    126:
    127:     /** Returns the 'from' parameter. */
    128:     public function getFrom() {
    129:         return $this->from;
    130:     }
    131:
    132:     /** Enables or disables dry mode. */
    133:     public function setDry($dry) {
    134:         $this->setBoolean('dry', $dry);
    135:     }
    136: }
    137: