Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

102 linhas
3.1KB

  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. */
  70. public function export()
  71. {
  72. // moved initialization of subject here because of the following issue
  73. // https://github.com/yiisoft/yii2/issues/1446
  74. if (empty($this->message['subject'])) {
  75. $this->message['subject'] = 'Application Log';
  76. }
  77. $messages = array_map([$this, 'formatMessage'], $this->messages);
  78. $body = wordwrap(implode("\n", $messages), 70);
  79. $this->composeMessage($body)->send($this->mailer);
  80. }
  81. /**
  82. * Composes a mail message with the given body content.
  83. * @param string $body the body content
  84. * @return \yii\mail\MessageInterface $message
  85. */
  86. protected function composeMessage($body)
  87. {
  88. $message = $this->mailer->compose();
  89. Yii::configure($message, $this->message);
  90. $message->setTextBody($body);
  91. return $message;
  92. }
  93. }