123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- /**
- * CEmailLogRoute class file.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @link http://www.yiiframework.com/
- * @copyright 2008-2013 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- /**
- * CEmailLogRoute sends selected log messages to email addresses.
- *
- * The target email addresses may be specified via {@link setEmails emails} property.
- * Optionally, you may set the email {@link setSubject subject}, the
- * {@link setSentFrom sentFrom} address and any additional {@link setHeaders headers}.
- *
- * @property array $emails List of destination email addresses.
- * @property string $subject Email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT.
- * @property string $sentFrom Send from address of the email.
- * @property array $headers Additional headers to use when sending an email.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @package system.logging
- * @since 1.0
- */
- class CEmailLogRoute extends CLogRoute
- {
- /**
- * @var boolean set this property to true value in case log data you're going to send through emails contains
- * non-latin or UTF-8 characters. Emails would be UTF-8 encoded.
- * @since 1.1.13
- */
- public $utf8=false;
- /**
- * @var array list of destination email addresses.
- */
- private $_email=array();
- /**
- * @var string email subject
- */
- private $_subject;
- /**
- * @var string email sent from address
- */
- private $_from;
- /**
- * @var array list of additional headers to use when sending an email.
- */
- private $_headers=array();
- /**
- * Sends log messages to specified email addresses.
- * @param array $logs list of log messages
- */
- protected function processLogs($logs)
- {
- $message='';
- foreach($logs as $log)
- $message.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
- $message=wordwrap($message,70);
- $subject=$this->getSubject();
- if($subject===null)
- $subject=Yii::t('yii','Application Log');
- foreach($this->getEmails() as $email)
- $this->sendEmail($email,$subject,$message);
- }
- /**
- * Sends an email.
- * @param string $email single email address
- * @param string $subject email subject
- * @param string $message email content
- */
- protected function sendEmail($email,$subject,$message)
- {
- $headers=$this->getHeaders();
- if($this->utf8)
- {
- $headers[]="MIME-Version: 1.0";
- $headers[]="Content-Type: text/plain; charset=UTF-8";
- $subject='=?UTF-8?B?'.base64_encode($subject).'?=';
- }
- if(($from=$this->getSentFrom())!==null)
- {
- $matches=array();
- preg_match_all('/([^<]*)<([^>]*)>/iu',$from,$matches);
- if(isset($matches[1][0],$matches[2][0]))
- {
- $name=$this->utf8 ? '=?UTF-8?B?'.base64_encode(trim($matches[1][0])).'?=' : trim($matches[1][0]);
- $from=trim($matches[2][0]);
- $headers[]="From: {$name} <{$from}>";
- }
- else
- $headers[]="From: {$from}";
- $headers[]="Reply-To: {$from}";
- }
- mail($email,$subject,$message,implode("\r\n",$headers));
- }
- /**
- * @return array list of destination email addresses
- */
- public function getEmails()
- {
- return $this->_email;
- }
- /**
- * @param mixed $value list of destination email addresses. If the value is
- * a string, it is assumed to be comma-separated email addresses.
- */
- public function setEmails($value)
- {
- if(is_array($value))
- $this->_email=$value;
- else
- $this->_email=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY);
- }
- /**
- * @return string email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT
- */
- public function getSubject()
- {
- return $this->_subject;
- }
- /**
- * @param string $value email subject.
- */
- public function setSubject($value)
- {
- $this->_subject=$value;
- }
- /**
- * @return string send from address of the email
- */
- public function getSentFrom()
- {
- return $this->_from;
- }
- /**
- * @param string $value send from address of the email
- */
- public function setSentFrom($value)
- {
- $this->_from=$value;
- }
- /**
- * @return array additional headers to use when sending an email.
- * @since 1.1.4
- */
- public function getHeaders()
- {
- return $this->_headers;
- }
- /**
- * @param mixed $value list of additional headers to use when sending an email.
- * If the value is a string, it is assumed to be line break separated headers.
- * @since 1.1.4
- */
- public function setHeaders($value)
- {
- if (is_array($value))
- $this->_headers=$value;
- else
- $this->_headers=preg_split('/\r\n|\n/',$value,-1,PREG_SPLIT_NO_EMPTY);
- }
- }
|