NativeMailerHandler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. 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 array
  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 array
  34. */
  35. protected $headers = array();
  36. /**
  37. * Optional parameters for the message
  38. * @var array
  39. */
  40. protected $parameters = array();
  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
  49. */
  50. protected $contentType = 'text/plain';
  51. /**
  52. * The encoding for the message
  53. * @var string
  54. */
  55. protected $encoding = 'utf-8';
  56. /**
  57. * @param string|array $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 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, $subject, $from, $level = Logger::ERROR, $bubble = true, $maxColumnWidth = 70)
  65. {
  66. parent::__construct($level, $bubble);
  67. $this->to = is_array($to) ? $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|array $headers Custom added headers
  76. * @return self
  77. */
  78. public function addHeader($headers)
  79. {
  80. foreach ((array) $headers as $header) {
  81. if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
  82. throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
  83. }
  84. $this->headers[] = $header;
  85. }
  86. return $this;
  87. }
  88. /**
  89. * Add parameters to the message
  90. *
  91. * @param string|array $parameters Custom added parameters
  92. * @return self
  93. */
  94. public function addParameter($parameters)
  95. {
  96. $this->parameters = array_merge($this->parameters, (array) $parameters);
  97. return $this;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function send($content, array $records)
  103. {
  104. $content = wordwrap($content, $this->maxColumnWidth);
  105. $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
  106. $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n";
  107. if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
  108. $headers .= 'MIME-Version: 1.0' . "\r\n";
  109. }
  110. $subject = $this->subject;
  111. if ($records) {
  112. $subjectFormatter = new LineFormatter($this->subject);
  113. $subject = $subjectFormatter->format($this->getHighestRecord($records));
  114. }
  115. $parameters = implode(' ', $this->parameters);
  116. foreach ($this->to as $to) {
  117. mail($to, $subject, $content, $headers, $parameters);
  118. }
  119. }
  120. /**
  121. * @return string $contentType
  122. */
  123. public function getContentType()
  124. {
  125. return $this->contentType;
  126. }
  127. /**
  128. * @return string $encoding
  129. */
  130. public function getEncoding()
  131. {
  132. return $this->encoding;
  133. }
  134. /**
  135. * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML
  136. * messages.
  137. * @return self
  138. */
  139. public function setContentType($contentType)
  140. {
  141. if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
  142. throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
  143. }
  144. $this->contentType = $contentType;
  145. return $this;
  146. }
  147. /**
  148. * @param string $encoding
  149. * @return self
  150. */
  151. public function setEncoding($encoding)
  152. {
  153. if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
  154. throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
  155. }
  156. $this->encoding = $encoding;
  157. return $this;
  158. }
  159. }