NativeMailerHandler.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 null
  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. }
  86. /**
  87. * Add parameters to the message
  88. *
  89. * @param string|array $arguments Custom added parameters
  90. * @return self
  91. */
  92. public function addParameter($parameters)
  93. {
  94. $this->parameters = array_merge($this->parameters, (array) $parameters);
  95. return $this;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. protected function send($content, array $records)
  101. {
  102. $content = wordwrap($content, $this->maxColumnWidth);
  103. $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
  104. $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n";
  105. if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
  106. $headers .= 'MIME-Version: 1.0' . "\r\n";
  107. }
  108. foreach ($this->to as $to) {
  109. mail($to, $this->subject, $content, $headers, implode(' ', $this->parameters));
  110. }
  111. }
  112. /**
  113. * @return string $contentType
  114. */
  115. public function getContentType()
  116. {
  117. return $this->contentType;
  118. }
  119. /**
  120. * @return string $encoding
  121. */
  122. public function getEncoding()
  123. {
  124. return $this->encoding;
  125. }
  126. /**
  127. * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML
  128. * messages.
  129. * @return self
  130. */
  131. public function setContentType($contentType)
  132. {
  133. if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
  134. throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
  135. }
  136. $this->contentType = $contentType;
  137. return $this;
  138. }
  139. /**
  140. * @param string $encoding
  141. * @return self
  142. */
  143. public function setEncoding($encoding)
  144. {
  145. if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
  146. throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
  147. }
  148. $this->encoding = $encoding;
  149. return $this;
  150. }
  151. }