ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
12namespace Monolog\Handler;
13
15
23{
28 protected $to;
29
34 protected $subject;
35
40 protected $headers = array();
41
46 protected $parameters = array();
47
52 protected $maxColumnWidth;
53
58 protected $contentType = 'text/plain';
59
64 protected $encoding = 'utf-8';
65
74 public function __construct($to, $subject, $from, $level = Logger::ERROR, $bubble = true, $maxColumnWidth = 70)
75 {
76 parent::__construct($level, $bubble);
77 $this->to = is_array($to) ? $to : array($to);
78 $this->subject = $subject;
79 $this->addHeader(sprintf('From: %s', $from));
80 $this->maxColumnWidth = $maxColumnWidth;
81 }
82
89 public function addHeader($headers)
90 {
91 foreach ((array) $headers as $header) {
92 if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
93 throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
94 }
95 $this->headers[] = $header;
96 }
97
98 return $this;
99 }
100
107 public function addParameter($parameters)
108 {
109 $this->parameters = array_merge($this->parameters, (array) $parameters);
110
111 return $this;
112 }
113
117 protected function send($content, array $records)
118 {
119 $content = wordwrap($content, $this->maxColumnWidth);
120 $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
121 $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n";
122 if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
123 $headers .= 'MIME-Version: 1.0' . "\r\n";
124 }
125 foreach ($this->to as $to) {
126 mail($to, $this->subject, $content, $headers, implode(' ', $this->parameters));
127 }
128 }
129
133 public function getContentType()
134 {
135 return $this->contentType;
136 }
137
141 public function getEncoding()
142 {
143 return $this->encoding;
144 }
145
152 {
153 if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
154 throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
155 }
156
157 $this->contentType = $contentType;
158
159 return $this;
160 }
161
166 public function setEncoding($encoding)
167 {
168 if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
169 throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
170 }
171
172 $this->encoding = $encoding;
173
174 return $this;
175 }
176}
Base class for all mail handlers.
Definition: MailHandler.php:20
NativeMailerHandler uses the mail() function to send the emails.
__construct($to, $subject, $from, $level=Logger::ERROR, $bubble=true, $maxColumnWidth=70)
addHeader($headers)
Add headers to the message.
send($content, array $records)
{Send a mail with the given content.}
addParameter($parameters)
Add parameters to the message.
Monolog log channel.
Definition: Logger.php:28
const ERROR
Runtime errors.
Definition: Logger.php:57
$header
$records
Definition: simple_test.php:17