NativeMailerHandler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. use Monolog\Formatter\LineFormatter;
  13. /**
  14. * NativeMailerHandler uses the mail() function to send the emails
  15. *
  16. * @author Christophe Coevoet <stof@notk.org>
  17. * @author Mark Garrett <mark@moderndeveloperllc.com>
  18. */
  19. class NativeMailerHandler extends MailHandler
  20. {
  21. /**
  22. * The email addresses to which the message will be sent
  23. * @var string[]
  24. */
  25. protected $to;
  26. /**
  27. * The subject of the email
  28. * @var string
  29. */
  30. protected $subject;
  31. /**
  32. * Optional headers for the message
  33. * @var string[]
  34. */
  35. protected $headers = [];
  36. /**
  37. * Optional parameters for the message
  38. * @var string[]
  39. */
  40. protected $parameters = [];
  41. /**
  42. * The wordwrap length for the message
  43. * @var int
  44. */
  45. protected $maxColumnWidth;
  46. /**
  47. * The Content-type for the message
  48. * @var string|null
  49. */
  50. protected $contentType;
  51. /**
  52. * The encoding for the message
  53. * @var string
  54. */
  55. protected $encoding = 'utf-8';
  56. /**
  57. * @param string|string[] $to The receiver of the mail
  58. * @param string $subject The subject of the mail
  59. * @param string $from The sender of the mail
  60. * @param string|int $level The minimum logging level at which this handler will be triggered
  61. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  62. * @param int $maxColumnWidth The maximum column width that the message lines will have
  63. */
  64. public function __construct($to, string $subject, string $from, $level = Logger::ERROR, bool $bubble = true, int $maxColumnWidth = 70)
  65. {
  66. parent::__construct($level, $bubble);
  67. $this->to = (array) $to;
  68. $this->subject = $subject;
  69. $this->addHeader(sprintf('From: %s', $from));
  70. $this->maxColumnWidth = $maxColumnWidth;
  71. }
  72. /**
  73. * Add headers to the message
  74. *
  75. * @param string|string[] $headers Custom added headers
  76. */
  77. public function addHeader($headers): self
  78. {
  79. foreach ((array) $headers as $header) {
  80. if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
  81. throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
  82. }
  83. $this->headers[] = $header;
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Add parameters to the message
  89. *
  90. * @param string|string[] $parameters Custom added parameters
  91. */
  92. public function addParameter($parameters): self
  93. {
  94. $this->parameters = array_merge($this->parameters, (array) $parameters);
  95. return $this;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. protected function send(string $content, array $records): void
  101. {
  102. $contentType = $this->getContentType() ?: ($this->isHtmlBody($content) ? 'text/html' : 'text/plain');
  103. if ($contentType !== 'text/html') {
  104. $content = wordwrap($content, $this->maxColumnWidth);
  105. }
  106. $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
  107. $headers .= 'Content-type: ' . $contentType . '; charset=' . $this->getEncoding() . "\r\n";
  108. if ($contentType === 'text/html' && false === strpos($headers, 'MIME-Version:')) {
  109. $headers .= 'MIME-Version: 1.0' . "\r\n";
  110. }
  111. $subject = $this->subject;
  112. if ($records) {
  113. $subjectFormatter = new LineFormatter($this->subject);
  114. $subject = $subjectFormatter->format($this->getHighestRecord($records));
  115. }
  116. $parameters = implode(' ', $this->parameters);
  117. foreach ($this->to as $to) {
  118. mail($to, $subject, $content, $headers, $parameters);
  119. }
  120. }
  121. public function getContentType(): ?string
  122. {
  123. return $this->contentType;
  124. }
  125. public function getEncoding(): string
  126. {
  127. return $this->encoding;
  128. }
  129. /**
  130. * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML messages.
  131. */
  132. public function setContentType(string $contentType): self
  133. {
  134. if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
  135. throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
  136. }
  137. $this->contentType = $contentType;
  138. return $this;
  139. }
  140. public function setEncoding(string $encoding): self
  141. {
  142. if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
  143. throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
  144. }
  145. $this->encoding = $encoding;
  146. return $this;
  147. }
  148. }