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.

74 lines
1.9 KiB

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