LoggerAppenderMail.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage appenders
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
  24. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  25. /**
  26. * Appends log events to mail using php function {@link PHP_MANUAL#mail}.
  27. *
  28. * <p>Parameters are {@link $from}, {@link $to}, {@link $subject}.</p>
  29. * <p>This appender requires a layout.</p>
  30. *
  31. * @author VxR <vxr@vxr.it>
  32. * @version $Revision: 1.1 $
  33. * @package log4php
  34. * @subpackage appenders
  35. */
  36. class LoggerAppenderMail extends LoggerAppenderSkeleton {
  37. /**
  38. * @var string 'from' field
  39. */
  40. var $from = null;
  41. /**
  42. * @var string 'subject' field
  43. */
  44. var $subject = 'Log4php Report';
  45. /**
  46. * @var string 'to' field
  47. */
  48. var $to = null;
  49. /**
  50. * @var string used to create mail body
  51. * @access private
  52. */
  53. var $body = '';
  54. /**
  55. * @access private
  56. */
  57. var $requiresLayout = true;
  58. /**
  59. * Constructor.
  60. *
  61. * @param string $name appender name
  62. */
  63. function LoggerAppenderMail($name)
  64. {
  65. $this->LoggerAppenderSkeleton($name);
  66. }
  67. function activateOptions()
  68. {
  69. $this->closed = false;
  70. return;
  71. }
  72. function close()
  73. {
  74. $from = $this->getFrom();
  75. $to = $this->getTo();
  76. if (!empty($this->body) and $from !== null and $to !== null and $this->layout !== null) {
  77. $subject = $this->getSubject();
  78. LoggerLog::debug("LoggerAppenderMail::close() sending mail from=[{$from}] to=[{$to}] subject=[{$subject}]");
  79. @mail(
  80. $to, $subject,
  81. $this->layout->getHeader() . $this->body . $this->layout->getFooter(),
  82. "From: {$from}\r\n"
  83. );
  84. }
  85. $this->closed = true;
  86. }
  87. /**
  88. * @return string
  89. */
  90. function getFrom()
  91. {
  92. return $this->from;
  93. }
  94. /**
  95. * @return string
  96. */
  97. function getSubject()
  98. {
  99. return $this->subject;
  100. }
  101. /**
  102. * @return string
  103. */
  104. function getTo()
  105. {
  106. return $this->to;
  107. }
  108. function setSubject($subject)
  109. {
  110. $this->subject = $subject;
  111. }
  112. function setTo($to)
  113. {
  114. $this->to = $to;
  115. }
  116. function setFrom($from)
  117. {
  118. $this->from = $from;
  119. }
  120. function append($event)
  121. {
  122. if ($this->layout !== null)
  123. $this->body .= $this->layout->format($event);
  124. }
  125. }
  126. ?>