Mail.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage Writer
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Log_Writer_Abstract */
  23. require_once 'Zend/Log/Writer/Abstract.php';
  24. /** Zend_Log_Exception */
  25. require_once 'Zend/Log/Exception.php';
  26. /** Zend_Log_Formatter_Simple*/
  27. require_once 'Zend/Log/Formatter/Simple.php';
  28. /**
  29. * Class used for writing log messages to email via Zend_Mail.
  30. *
  31. * Allows for emailing log messages at and above a certain level via a
  32. * Zend_Mail object. Note that this class only sends the email upon
  33. * completion, so any log entries accumulated are sent in a single email.
  34. *
  35. * @category Zend
  36. * @package Zend_Log
  37. * @subpackage Writer
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @version $Id$
  41. */
  42. class Zend_Log_Writer_Mail extends Zend_Log_Writer_Abstract
  43. {
  44. /**
  45. * Array of formatted events to include in message body.
  46. *
  47. * @var array
  48. */
  49. protected $_eventsToMail = array();
  50. /**
  51. * Array of formatted lines for use in an HTML email body; these events
  52. * are formatted with an optional formatter if the caller is using
  53. * Zend_Layout.
  54. *
  55. * @var array
  56. */
  57. protected $_layoutEventsToMail = array();
  58. /**
  59. * Zend_Mail instance to use
  60. *
  61. * @var Zend_Mail
  62. */
  63. protected $_mail;
  64. /**
  65. * Zend_Layout instance to use; optional.
  66. *
  67. * @var Zend_Layout
  68. */
  69. protected $_layout;
  70. /**
  71. * Optional formatter for use when rendering with Zend_Layout.
  72. *
  73. * @var Zend_Log_Formatter_Interface
  74. */
  75. protected $_layoutFormatter;
  76. /**
  77. * Array keeping track of the number of entries per priority level.
  78. *
  79. * @var array
  80. */
  81. protected $_numEntriesPerPriority = array();
  82. /**
  83. * Subject prepend text.
  84. *
  85. * Can only be used of the Zend_Mail object has not already had its
  86. * subject line set. Using this will cause the subject to have the entry
  87. * counts per-priority level appended to it.
  88. *
  89. * @var string|null
  90. */
  91. protected $_subjectPrependText;
  92. /**
  93. * Class constructor.
  94. *
  95. * Constructs the mail writer; requires a Zend_Mail instance, and takes an
  96. * optional Zend_Layout instance. If Zend_Layout is being used,
  97. * $this->_layout->events will be set for use in the layout template.
  98. *
  99. * @param Zend_Mail $mail Mail instance
  100. * @param Zend_Layout $layout Layout instance; optional
  101. * @return void
  102. */
  103. public function __construct(Zend_Mail $mail, Zend_Layout $layout = null)
  104. {
  105. $this->_mail = $mail;
  106. $this->_layout = $layout;
  107. $this->_formatter = new Zend_Log_Formatter_Simple();
  108. }
  109. /**
  110. * Create a new instance of Zend_Log_Writer_Mail
  111. *
  112. * @exception Zend_Log_Exception
  113. * @param mixed $config
  114. * @return Zend_Log_Writer_Mail
  115. */
  116. static public function factory($config)
  117. {
  118. throw new Zend_Exception('TODO: Implement');
  119. // $config = self::_parseConfig($config);
  120. // $config = $config + array('mail'=>NULL, 'layout'=>NULL);
  121. //
  122. // return new self(
  123. // $config['mail'],
  124. // $config['layout']
  125. // );
  126. }
  127. /**
  128. * Places event line into array of lines to be used as message body.
  129. *
  130. * Handles the formatting of both plaintext entries, as well as those
  131. * rendered with Zend_Layout.
  132. *
  133. * @param array $event Event data
  134. * @return void
  135. */
  136. protected function _write($event)
  137. {
  138. // Track the number of entries per priority level.
  139. if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
  140. $this->_numEntriesPerPriority[$event['priorityName']] = 1;
  141. } else {
  142. $this->_numEntriesPerPriority[$event['priorityName']]++;
  143. }
  144. $formattedEvent = $this->_formatter->format($event);
  145. // All plaintext events are to use the standard formatter.
  146. $this->_eventsToMail[] = $formattedEvent;
  147. // If we have a Zend_Layout instance, use a specific formatter for the
  148. // layout if one exists. Otherwise, just use the event with its
  149. // default format.
  150. if ($this->_layout) {
  151. if ($this->_layoutFormatter) {
  152. $this->_layoutEventsToMail[] =
  153. $this->_layoutFormatter->format($event);
  154. } else {
  155. $this->_layoutEventsToMail[] = $formattedEvent;
  156. }
  157. }
  158. }
  159. /**
  160. * Gets instance of Zend_Log_Formatter_Instance used for formatting a
  161. * message using Zend_Layout, if applicable.
  162. *
  163. * @return Zend_Log_Formatter_Interface|null The formatter, or null.
  164. */
  165. public function getLayoutFormatter()
  166. {
  167. return $this->_layoutFormatter;
  168. }
  169. /**
  170. * Sets a specific formatter for use with Zend_Layout events.
  171. *
  172. * Allows use of a second formatter on lines that will be rendered with
  173. * Zend_Layout. In the event that Zend_Layout is not being used, this
  174. * formatter cannot be set, so an exception will be thrown.
  175. *
  176. * @param Zend_Log_Formatter_Interface $formatter
  177. * @return Zend_Log_Writer_Mail
  178. * @throws Zend_Log_Exception
  179. */
  180. public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter)
  181. {
  182. if (!$this->_layout) {
  183. throw new Zend_Log_Exception(
  184. 'cannot set formatter for layout; ' .
  185. 'a Zend_Layout instance is not in use');
  186. }
  187. $this->_layoutFormatter = $formatter;
  188. return $this;
  189. }
  190. /**
  191. * Allows caller to have the mail subject dynamically set to contain the
  192. * entry counts per-priority level.
  193. *
  194. * Sets the text for use in the subject, with entry counts per-priority
  195. * level appended to the end. Since a Zend_Mail subject can only be set
  196. * once, this method cannot be used if the Zend_Mail object already has a
  197. * subject set.
  198. *
  199. * @param string $subject Subject prepend text.
  200. * @return Zend_Log_Writer_Mail
  201. */
  202. public function setSubjectPrependText($subject)
  203. {
  204. if ($this->_mail->getSubject()) {
  205. throw new Zend_Log_Exception(
  206. 'subject already set on mail; ' .
  207. 'cannot set subject prepend text');
  208. }
  209. $this->_subjectPrependText = (string) $subject;
  210. return $this;
  211. }
  212. /**
  213. * Sends mail to recipient(s) if log entries are present. Note that both
  214. * plaintext and HTML portions of email are handled here.
  215. *
  216. * @return void
  217. */
  218. public function shutdown()
  219. {
  220. // If there are events to mail, use them as message body. Otherwise,
  221. // there is no mail to be sent.
  222. if (empty($this->_eventsToMail)) {
  223. return;
  224. }
  225. if ($this->_subjectPrependText !== null) {
  226. // Tack on the summary of entries per-priority to the subject
  227. // line and set it on the Zend_Mail object.
  228. $numEntries = $this->_getFormattedNumEntriesPerPriority();
  229. $this->_mail->setSubject(
  230. "{$this->_subjectPrependText} ({$numEntries})");
  231. }
  232. // Always provide events to mail as plaintext.
  233. $this->_mail->setBodyText(implode('', $this->_eventsToMail));
  234. // If a Zend_Layout instance is being used, set its "events"
  235. // value to the lines formatted for use with the layout.
  236. if ($this->_layout) {
  237. // Set the required "messages" value for the layout. Here we
  238. // are assuming that the layout is for use with HTML.
  239. $this->_layout->events =
  240. implode('', $this->_layoutEventsToMail);
  241. // If an exception occurs during rendering, convert it to a notice
  242. // so we can avoid an exception thrown without a stack frame.
  243. try {
  244. $this->_mail->setBodyHtml($this->_layout->render());
  245. } catch (Exception $e) {
  246. trigger_error(
  247. "exception occurred when rendering layout; " .
  248. "unable to set html body for message; " .
  249. "message = {$e->getMessage()}; " .
  250. "code = {$e->getCode()}; " .
  251. "exception class = " . get_class($e),
  252. E_USER_NOTICE);
  253. }
  254. }
  255. // Finally, send the mail. If an exception occurs, convert it into a
  256. // warning-level message so we can avoid an exception thrown without a
  257. // stack frame.
  258. try {
  259. $this->_mail->send();
  260. } catch (Exception $e) {
  261. trigger_error(
  262. "unable to send log entries via email; " .
  263. "message = {$e->getMessage()}; " .
  264. "code = {$e->getCode()}; " .
  265. "exception class = " . get_class($e),
  266. E_USER_WARNING);
  267. }
  268. }
  269. /**
  270. * Gets a string of number of entries per-priority level that occurred, or
  271. * an emptry string if none occurred.
  272. *
  273. * @return string
  274. */
  275. protected function _getFormattedNumEntriesPerPriority()
  276. {
  277. $strings = array();
  278. foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
  279. $strings[] = "{$priority}={$numEntries}";
  280. }
  281. return implode(', ', $strings);
  282. }
  283. }