EmailTarget.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\log;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\di\Instance;
  11. use yii\mail\MailerInterface;
  12. /**
  13. * EmailTarget sends selected log messages to the specified email addresses.
  14. *
  15. * You may configure the email to be sent by setting the [[message]] property, through which
  16. * you can set the target email addresses, subject, etc.:
  17. *
  18. * ```php
  19. * 'components' => [
  20. * 'log' => [
  21. * 'targets' => [
  22. * [
  23. * 'class' => 'yii\log\EmailTarget',
  24. * 'mailer' => 'mailer',
  25. * 'levels' => ['error', 'warning'],
  26. * 'message' => [
  27. * 'from' => ['log@example.com'],
  28. * 'to' => ['developer1@example.com', 'developer2@example.com'],
  29. * 'subject' => 'Log message',
  30. * ],
  31. * ],
  32. * ],
  33. * ],
  34. * ],
  35. * ```
  36. *
  37. * In the above `mailer` is ID of the component that sends email and should be already configured.
  38. *
  39. * @author Qiang Xue <qiang.xue@gmail.com>
  40. * @since 2.0
  41. */
  42. class EmailTarget extends Target
  43. {
  44. /**
  45. * @var array the configuration array for creating a [[\yii\mail\MessageInterface|message]] object.
  46. * Note that the "to" option must be set, which specifies the destination email address(es).
  47. */
  48. public $message = [];
  49. /**
  50. * @var MailerInterface|array|string the mailer object or the application component ID of the mailer object.
  51. * After the EmailTarget object is created, if you want to change this property, you should only assign it
  52. * with a mailer object.
  53. * Starting from version 2.0.2, this can also be a configuration array for creating the object.
  54. */
  55. public $mailer = 'mailer';
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function init()
  60. {
  61. parent::init();
  62. if (empty($this->message['to'])) {
  63. throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.');
  64. }
  65. $this->mailer = Instance::ensure($this->mailer, 'yii\mail\MailerInterface');
  66. }
  67. /**
  68. * Sends log messages to specified email addresses.
  69. * Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
  70. * @throws LogRuntimeException
  71. */
  72. public function export()
  73. {
  74. // moved initialization of subject here because of the following issue
  75. // https://github.com/yiisoft/yii2/issues/1446
  76. if (empty($this->message['subject'])) {
  77. $this->message['subject'] = 'Application Log';
  78. }
  79. $messages = array_map([$this, 'formatMessage'], $this->messages);
  80. $body = wordwrap(implode("\n", $messages), 70);
  81. $message = $this->composeMessage($body);
  82. if (!$message->send($this->mailer)) {
  83. throw new LogRuntimeException('Unable to export log through email!');
  84. }
  85. }
  86. /**
  87. * Composes a mail message with the given body content.
  88. * @param string $body the body content
  89. * @return \yii\mail\MessageInterface $message
  90. */
  91. protected function composeMessage($body)
  92. {
  93. $message = $this->mailer->compose();
  94. Yii::configure($message, $this->message);
  95. $message->setTextBody($body);
  96. return $message;
  97. }
  98. }