You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\log;
13 years ago
13 years ago
/**
13 years ago
* EmailTarget sends selected log messages to the specified email addresses.
13 years ago
*
13 years ago
* The target email addresses may be specified via [[emails]] property.
* Optionally, you may set the email [[subject]], [[sentFrom]] address and
* additional [[headers]].
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class EmailTarget extends Target
13 years ago
{
/**
* @var array list of destination email addresses.
*/
13 years ago
public $emails = array();
13 years ago
/**
* @var string email subject
*/
13 years ago
public $subject;
13 years ago
/**
13 years ago
* @var string email sent-from address
13 years ago
*/
13 years ago
public $sentFrom;
13 years ago
/**
* @var array list of additional headers to use when sending an email.
*/
13 years ago
public $headers = array();
13 years ago
/**
* Sends log messages to specified email addresses.
13 years ago
*/
12 years ago
public function export()
13 years ago
{
13 years ago
$body = '';
12 years ago
foreach ($this->messages as $message) {
13 years ago
$body .= $this->formatMessage($message);
}
$body = wordwrap($body, 70);
$subject = $this->subject === null ? \Yii::t('yii', 'Application Log') : $this->subject;
13 years ago
foreach ($this->emails as $email) {
$this->sendEmail($subject, $body, $email, $this->sentFrom, $this->headers);
}
13 years ago
}
/**
* Sends an email.
* @param string $subject email subject
13 years ago
* @param string $body email body
* @param string $sentTo sent-to email address
* @param string $sentFrom sent-from email address
* @param array $headers additional headers to be used when sending the email
13 years ago
*/
13 years ago
protected function sendEmail($subject, $body, $sentTo, $sentFrom, $headers)
13 years ago
{
13 years ago
if ($sentFrom !== null) {
$headers[] = "From: {$sentFrom}";
}
13 years ago
mail($sentTo, $subject, $body, implode("\r\n", $headers));
13 years ago
}
}