NativeMailerHandler.php 4.8 KB

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