scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 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; |
||
15 | use Monolog\Formatter\LineFormatter; |
||
16 | |||
17 | /** |
||
18 | * NativeMailerHandler uses the mail() function to send the emails |
||
19 | * |
||
20 | * @author Christophe Coevoet <stof@notk.org> |
||
21 | * @author Mark Garrett <mark@moderndeveloperllc.com> |
||
22 | */ |
||
23 | class NativeMailerHandler extends MailHandler |
||
24 | { |
||
25 | /** |
||
26 | * The email addresses to which the message will be sent |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $to; |
||
30 | |||
31 | /** |
||
32 | * The subject of the email |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $subject; |
||
36 | |||
37 | /** |
||
38 | * Optional headers for the message |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $headers = array(); |
||
42 | |||
43 | /** |
||
44 | * Optional parameters for the message |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $parameters = array(); |
||
48 | |||
49 | /** |
||
50 | * The wordwrap length for the message |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $maxColumnWidth; |
||
54 | |||
55 | /** |
||
56 | * The Content-type for the message |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $contentType = 'text/plain'; |
||
60 | |||
61 | /** |
||
62 | * The encoding for the message |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $encoding = 'utf-8'; |
||
66 | |||
67 | /** |
||
68 | * @param string|array $to The receiver of the mail |
||
69 | * @param string $subject The subject of the mail |
||
70 | * @param string $from The sender of the mail |
||
71 | * @param int $level The minimum logging level at which this handler will be triggered |
||
72 | * @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
||
73 | * @param int $maxColumnWidth The maximum column width that the message lines will have |
||
74 | */ |
||
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 | |||
84 | /** |
||
85 | * Add headers to the message |
||
86 | * |
||
87 | * @param string|array $headers Custom added headers |
||
88 | * @return self |
||
89 | */ |
||
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 | |||
102 | /** |
||
103 | * Add parameters to the message |
||
104 | * |
||
105 | * @param string|array $parameters Custom added parameters |
||
106 | * @return self |
||
107 | */ |
||
108 | public function addParameter($parameters) |
||
109 | { |
||
110 | $this->parameters = array_merge($this->parameters, (array) $parameters); |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
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 | |||
127 | $subject = $this->subject; |
||
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 | |||
139 | /** |
||
140 | * @return string $contentType |
||
141 | */ |
||
142 | public function getContentType() |
||
143 | { |
||
144 | return $this->contentType; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return string $encoding |
||
149 | */ |
||
150 | public function getEncoding() |
||
151 | { |
||
152 | return $this->encoding; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML |
||
157 | * messages. |
||
158 | * @return self |
||
159 | */ |
||
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 | |||
171 | /** |
||
172 | * @param string $encoding |
||
173 | * @return self |
||
174 | */ |
||
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 | } |