ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
NativeMailerHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
14 use Monolog\Logger;
16 
24 {
29  protected $to;
30 
35  protected $subject;
36 
41  protected $headers = array();
42 
47  protected $parameters = array();
48 
53  protected $maxColumnWidth;
54 
59  protected $contentType = 'text/plain';
60 
65  protected $encoding = 'utf-8';
66 
75  public function __construct($to, $subject, $from, $level = Logger::ERROR, $bubble = true, $maxColumnWidth = 70)
76  {
77  parent::__construct($level, $bubble);
78  $this->to = is_array($to) ? $to : array($to);
79  $this->subject = $subject;
80  $this->addHeader(sprintf('From: %s', $from));
81  $this->maxColumnWidth = $maxColumnWidth;
82  }
83 
90  public function addHeader($headers)
91  {
92  foreach ((array) $headers as $header) {
93  if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
94  throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
95  }
96  $this->headers[] = $header;
97  }
98 
99  return $this;
100  }
101 
108  public function addParameter($parameters)
109  {
110  $this->parameters = array_merge($this->parameters, (array) $parameters);
111 
112  return $this;
113  }
114 
118  protected function send($content, array $records)
119  {
120  $content = wordwrap($content, $this->maxColumnWidth);
121  $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
122  $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n";
123  if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
124  $headers .= 'MIME-Version: 1.0' . "\r\n";
125  }
126 
128  if ($records) {
129  $subjectFormatter = new LineFormatter($this->subject);
130  $subject = $subjectFormatter->format($this->getHighestRecord($records));
131  }
132 
133  $parameters = implode(' ', $this->parameters);
134  foreach ($this->to as $to) {
135  mail($to, $subject, $content, $headers, $parameters);
136  }
137  }
138 
142  public function getContentType()
143  {
144  return $this->contentType;
145  }
146 
150  public function getEncoding()
151  {
152  return $this->encoding;
153  }
154 
160  public function setContentType($contentType)
161  {
162  if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
163  throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
164  }
165 
166  $this->contentType = $contentType;
167 
168  return $this;
169  }
170 
175  public function setEncoding($encoding)
176  {
177  if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
178  throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
179  }
180 
181  $this->encoding = $encoding;
182 
183  return $this;
184  }
185 }
File written to
send($content, array $records)
{}
const ERROR
Runtime errors.
Definition: Logger.php:57
getHighestRecord(array $records)
Definition: MailHandler.php:56
$records
Definition: simple_test.php:22
mail($to, $subject, $message, $additional_headers=null, $additional_parameters=null)
NativeMailerHandler uses the mail() function to send the emails.
addHeader($headers)
Add headers to the message.
__construct($to, $subject, $from, $level=Logger::ERROR, $bubble=true, $maxColumnWidth=70)
addParameter($parameters)
Add parameters to the message.
$header
Create styles array
The data for the language used.
Base class for all mail handlers.
Definition: MailHandler.php:19
Formats incoming records into a one-line string.