Mail.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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-2010 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-2010 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. * @param array|Zend_Config $config
  113. * @return Zend_Log_Writer_Mail
  114. * @throws Zend_Log_Exception
  115. */
  116. static public function factory($config)
  117. {
  118. throw new Zend_Exception('Zend_Log_Writer_Mail does not currently implement a factory');
  119. }
  120. /**
  121. * Places event line into array of lines to be used as message body.
  122. *
  123. * Handles the formatting of both plaintext entries, as well as those
  124. * rendered with Zend_Layout.
  125. *
  126. * @param array $event Event data
  127. * @return void
  128. */
  129. protected function _write($event)
  130. {
  131. // Track the number of entries per priority level.
  132. if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) {
  133. $this->_numEntriesPerPriority[$event['priorityName']] = 1;
  134. } else {
  135. $this->_numEntriesPerPriority[$event['priorityName']]++;
  136. }
  137. $formattedEvent = $this->_formatter->format($event);
  138. // All plaintext events are to use the standard formatter.
  139. $this->_eventsToMail[] = $formattedEvent;
  140. // If we have a Zend_Layout instance, use a specific formatter for the
  141. // layout if one exists. Otherwise, just use the event with its
  142. // default format.
  143. if ($this->_layout) {
  144. if ($this->_layoutFormatter) {
  145. $this->_layoutEventsToMail[] =
  146. $this->_layoutFormatter->format($event);
  147. } else {
  148. $this->_layoutEventsToMail[] = $formattedEvent;
  149. }
  150. }
  151. }
  152. /**
  153. * Gets instance of Zend_Log_Formatter_Instance used for formatting a
  154. * message using Zend_Layout, if applicable.
  155. *
  156. * @return Zend_Log_Formatter_Interface|null The formatter, or null.
  157. */
  158. public function getLayoutFormatter()
  159. {
  160. return $this->_layoutFormatter;
  161. }
  162. /**
  163. * Sets a specific formatter for use with Zend_Layout events.
  164. *
  165. * Allows use of a second formatter on lines that will be rendered with
  166. * Zend_Layout. In the event that Zend_Layout is not being used, this
  167. * formatter cannot be set, so an exception will be thrown.
  168. *
  169. * @param Zend_Log_Formatter_Interface $formatter
  170. * @return Zend_Log_Writer_Mail
  171. * @throws Zend_Log_Exception
  172. */
  173. public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter)
  174. {
  175. if (!$this->_layout) {
  176. throw new Zend_Log_Exception(
  177. 'cannot set formatter for layout; ' .
  178. 'a Zend_Layout instance is not in use');
  179. }
  180. $this->_layoutFormatter = $formatter;
  181. return $this;
  182. }
  183. /**
  184. * Allows caller to have the mail subject dynamically set to contain the
  185. * entry counts per-priority level.
  186. *
  187. * Sets the text for use in the subject, with entry counts per-priority
  188. * level appended to the end. Since a Zend_Mail subject can only be set
  189. * once, this method cannot be used if the Zend_Mail object already has a
  190. * subject set.
  191. *
  192. * @param string $subject Subject prepend text.
  193. * @return Zend_Log_Writer_Mail
  194. */
  195. public function setSubjectPrependText($subject)
  196. {
  197. if ($this->_mail->getSubject()) {
  198. throw new Zend_Log_Exception(
  199. 'subject already set on mail; ' .
  200. 'cannot set subject prepend text');
  201. }
  202. $this->_subjectPrependText = (string) $subject;
  203. return $this;
  204. }
  205. /**
  206. * Sends mail to recipient(s) if log entries are present. Note that both
  207. * plaintext and HTML portions of email are handled here.
  208. *
  209. * @return void
  210. */
  211. public function shutdown()
  212. {
  213. // If there are events to mail, use them as message body. Otherwise,
  214. // there is no mail to be sent.
  215. if (empty($this->_eventsToMail)) {
  216. return;
  217. }
  218. if ($this->_subjectPrependText !== null) {
  219. // Tack on the summary of entries per-priority to the subject
  220. // line and set it on the Zend_Mail object.
  221. $numEntries = $this->_getFormattedNumEntriesPerPriority();
  222. $this->_mail->setSubject(
  223. "{$this->_subjectPrependText} ({$numEntries})");
  224. }
  225. // Always provide events to mail as plaintext.
  226. $this->_mail->setBodyText(implode('', $this->_eventsToMail));
  227. // If a Zend_Layout instance is being used, set its "events"
  228. // value to the lines formatted for use with the layout.
  229. if ($this->_layout) {
  230. // Set the required "messages" value for the layout. Here we
  231. // are assuming that the layout is for use with HTML.
  232. $this->_layout->events =
  233. implode('', $this->_layoutEventsToMail);
  234. // If an exception occurs during rendering, convert it to a notice
  235. // so we can avoid an exception thrown without a stack frame.
  236. try {
  237. $this->_mail->setBodyHtml($this->_layout->render());
  238. } catch (Exception $e) {
  239. trigger_error(
  240. "exception occurred when rendering layout; " .
  241. "unable to set html body for message; " .
  242. "message = {$e->getMessage()}; " .
  243. "code = {$e->getCode()}; " .
  244. "exception class = " . get_class($e),
  245. E_USER_NOTICE);
  246. }
  247. }
  248. // Finally, send the mail. If an exception occurs, convert it into a
  249. // warning-level message so we can avoid an exception thrown without a
  250. // stack frame.
  251. try {
  252. $this->_mail->send();
  253. } catch (Exception $e) {
  254. trigger_error(
  255. "unable to send log entries via email; " .
  256. "message = {$e->getMessage()}; " .
  257. "code = {$e->getCode()}; " .
  258. "exception class = " . get_class($e),
  259. E_USER_WARNING);
  260. }
  261. }
  262. /**
  263. * Gets a string of number of entries per-priority level that occurred, or
  264. * an emptry string if none occurred.
  265. *
  266. * @return string
  267. */
  268. protected function _getFormattedNumEntriesPerPriority()
  269. {
  270. $strings = array();
  271. foreach ($this->_numEntriesPerPriority as $priority => $numEntries) {
  272. $strings[] = "{$priority}={$numEntries}";
  273. }
  274. return implode(', ', $strings);
  275. }
  276. }