NativeMailerHandler.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  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. /**
  13. * NativeMailerHandler uses the mail() function to send the emails
  14. *
  15. * @author Christophe Coevoet <stof@notk.org>
  16. * @author Mark Garrett <mark@moderndeveloperllc.com>
  17. */
  18. class NativeMailerHandler extends MailHandler
  19. {
  20. /**
  21. * The email addresses to which the message will be sent
  22. * @var array
  23. */
  24. protected $to;
  25. /**
  26. * The subject of the email
  27. * @var string
  28. */
  29. protected $subject;
  30. /**
  31. * Optional headers for the message
  32. * @var array
  33. */
  34. protected $headers = array();
  35. /**
  36. * Optional parameters for the message
  37. * @var array
  38. */
  39. protected $parameters = array();
  40. /**
  41. * The wordwrap length for the message
  42. * @var integer
  43. */
  44. protected $maxColumnWidth;
  45. /**
  46. * The Content-type for the message
  47. * @var string
  48. */
  49. protected $contentType = 'text/plain';
  50. /**
  51. * The encoding for the message
  52. * @var string
  53. */
  54. protected $encoding = 'utf-8';
  55. /**
  56. * @param string|array $to The receiver of the mail
  57. * @param string $subject The subject of the mail
  58. * @param string $from The sender of the mail
  59. * @param integer $level The minimum logging level at which this handler will be triggered
  60. * @param boolean $bubble Whether the messages that are handled can bubble up the stack or not
  61. * @param int $maxColumnWidth The maximum column width that the message lines will have
  62. */
  63. public function __construct($to, $subject, $from, $level = Logger::ERROR, $bubble = true, $maxColumnWidth = 70)
  64. {
  65. parent::__construct($level, $bubble);
  66. $this->to = is_array($to) ? $to : array($to);
  67. $this->subject = $subject;
  68. $this->addHeader(sprintf('From: %s', $from));
  69. $this->maxColumnWidth = $maxColumnWidth;
  70. }
  71. /**
  72. * Add headers to the message
  73. *
  74. * @param string|array $headers Custom added headers
  75. * @return self
  76. */
  77. public function addHeader($headers)
  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|array $parameters Custom added parameters
  91. * @return self
  92. */
  93. public function addParameter($parameters)
  94. {
  95. $this->parameters = array_merge($this->parameters, (array) $parameters);
  96. return $this;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. protected function send($content, array $records)
  102. {
  103. $content = wordwrap($content, $this->maxColumnWidth);
  104. $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
  105. $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n";
  106. if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
  107. $headers .= 'MIME-Version: 1.0' . "\r\n";
  108. }
  109. foreach ($this->to as $to) {
  110. mail($to, $this->subject, $content, $headers, implode(' ', $this->parameters));
  111. }
  112. }
  113. /**
  114. * @return string $contentType
  115. */
  116. public function getContentType()
  117. {
  118. return $this->contentType;
  119. }
  120. /**
  121. * @return string $encoding
  122. */
  123. public function getEncoding()
  124. {
  125. return $this->encoding;
  126. }
  127. /**
  128. * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML
  129. * messages.
  130. * @return self
  131. */
  132. public function setContentType($contentType)
  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. /**
  141. * @param string $encoding
  142. * @return self
  143. */
  144. public function setEncoding($encoding)
  145. {
  146. if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
  147. throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
  148. }
  149. $this->encoding = $encoding;
  150. return $this;
  151. }
  152. }