CEmailLogRoute.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * CEmailLogRoute class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CEmailLogRoute sends selected log messages to email addresses.
  12. *
  13. * The target email addresses may be specified via {@link setEmails emails} property.
  14. * Optionally, you may set the email {@link setSubject subject}, the
  15. * {@link setSentFrom sentFrom} address and any additional {@link setHeaders headers}.
  16. *
  17. * @property array $emails List of destination email addresses.
  18. * @property string $subject Email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT.
  19. * @property string $sentFrom Send from address of the email.
  20. * @property array $headers Additional headers to use when sending an email.
  21. *
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @package system.logging
  24. * @since 1.0
  25. */
  26. class CEmailLogRoute extends CLogRoute
  27. {
  28. /**
  29. * @var boolean set this property to true value in case log data you're going to send through emails contains
  30. * non-latin or UTF-8 characters. Emails would be UTF-8 encoded.
  31. * @since 1.1.13
  32. */
  33. public $utf8=false;
  34. /**
  35. * @var array list of destination email addresses.
  36. */
  37. private $_email=array();
  38. /**
  39. * @var string email subject
  40. */
  41. private $_subject;
  42. /**
  43. * @var string email sent from address
  44. */
  45. private $_from;
  46. /**
  47. * @var array list of additional headers to use when sending an email.
  48. */
  49. private $_headers=array();
  50. /**
  51. * Sends log messages to specified email addresses.
  52. * @param array $logs list of log messages
  53. */
  54. protected function processLogs($logs)
  55. {
  56. $message='';
  57. foreach($logs as $log)
  58. $message.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
  59. $message=wordwrap($message,70);
  60. $subject=$this->getSubject();
  61. if($subject===null)
  62. $subject=Yii::t('yii','Application Log');
  63. foreach($this->getEmails() as $email)
  64. $this->sendEmail($email,$subject,$message);
  65. }
  66. /**
  67. * Sends an email.
  68. * @param string $email single email address
  69. * @param string $subject email subject
  70. * @param string $message email content
  71. */
  72. protected function sendEmail($email,$subject,$message)
  73. {
  74. $headers=$this->getHeaders();
  75. if($this->utf8)
  76. {
  77. $headers[]="MIME-Version: 1.0";
  78. $headers[]="Content-Type: text/plain; charset=UTF-8";
  79. $subject='=?UTF-8?B?'.base64_encode($subject).'?=';
  80. }
  81. if(($from=$this->getSentFrom())!==null)
  82. {
  83. $matches=array();
  84. preg_match_all('/([^<]*)<([^>]*)>/iu',$from,$matches);
  85. if(isset($matches[1][0],$matches[2][0]))
  86. {
  87. $name=$this->utf8 ? '=?UTF-8?B?'.base64_encode(trim($matches[1][0])).'?=' : trim($matches[1][0]);
  88. $from=trim($matches[2][0]);
  89. $headers[]="From: {$name} <{$from}>";
  90. }
  91. else
  92. $headers[]="From: {$from}";
  93. $headers[]="Reply-To: {$from}";
  94. }
  95. mail($email,$subject,$message,implode("\r\n",$headers));
  96. }
  97. /**
  98. * @return array list of destination email addresses
  99. */
  100. public function getEmails()
  101. {
  102. return $this->_email;
  103. }
  104. /**
  105. * @param mixed $value list of destination email addresses. If the value is
  106. * a string, it is assumed to be comma-separated email addresses.
  107. */
  108. public function setEmails($value)
  109. {
  110. if(is_array($value))
  111. $this->_email=$value;
  112. else
  113. $this->_email=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY);
  114. }
  115. /**
  116. * @return string email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT
  117. */
  118. public function getSubject()
  119. {
  120. return $this->_subject;
  121. }
  122. /**
  123. * @param string $value email subject.
  124. */
  125. public function setSubject($value)
  126. {
  127. $this->_subject=$value;
  128. }
  129. /**
  130. * @return string send from address of the email
  131. */
  132. public function getSentFrom()
  133. {
  134. return $this->_from;
  135. }
  136. /**
  137. * @param string $value send from address of the email
  138. */
  139. public function setSentFrom($value)
  140. {
  141. $this->_from=$value;
  142. }
  143. /**
  144. * @return array additional headers to use when sending an email.
  145. * @since 1.1.4
  146. */
  147. public function getHeaders()
  148. {
  149. return $this->_headers;
  150. }
  151. /**
  152. * @param mixed $value list of additional headers to use when sending an email.
  153. * If the value is a string, it is assumed to be line break separated headers.
  154. * @since 1.1.4
  155. */
  156. public function setHeaders($value)
  157. {
  158. if (is_array($value))
  159. $this->_headers=$value;
  160. else
  161. $this->_headers=preg_split('/\r\n|\n/',$value,-1,PREG_SPLIT_NO_EMPTY);
  162. }
  163. }