PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / trunk
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance vtrunk
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / monolog / monolog / src / Monolog / Handler / NativeMailerHandler.php

NativeMailerHandler.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance trunk, at vendor/monolog/monolog/src/Monolog/Handler/NativeMailerHandler.php

175 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <[email protected]>
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 <[email protected]>
21 * @author Mark Garrett <[email protected]>
22 */
23 class NativeMailerHandler extends MailHandler
24 {
25 /**
26 * The email addresses to which the message will be sent
27 * @var string[]
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 string[]
40 */
41 protected $headers = [];
42
43 /**
44 * Optional parameters for the message
45 * @var string[]
46 */
47 protected $parameters = [];
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|null
58 */
59 protected $contentType;
60
61 /**
62 * The encoding for the message
63 * @var string
64 */
65 protected $encoding = 'utf-8';
66
67 /**
68 * @param string|string[] $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 $maxColumnWidth The maximum column width that the message lines will have
72 */
73 public function __construct($to, string $subject, string $from, $level = Logger::ERROR, bool $bubble = true, int $maxColumnWidth = 70)
74 {
75 parent::__construct($level, $bubble);
76 $this->to = (array) $to;
77 $this->subject = $subject;
78 $this->addHeader(sprintf('From: %s', $from));
79 $this->maxColumnWidth = $maxColumnWidth;
80 }
81
82 /**
83 * Add headers to the message
84 *
85 * @param string|string[] $headers Custom added headers
86 */
87 public function addHeader($headers): self
88 {
89 foreach ((array) $headers as $header) {
90 if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
91 throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
92 }
93 $this->headers[] = $header;
94 }
95
96 return $this;
97 }
98
99 /**
100 * Add parameters to the message
101 *
102 * @param string|string[] $parameters Custom added parameters
103 */
104 public function addParameter($parameters): self
105 {
106 $this->parameters = array_merge($this->parameters, (array) $parameters);
107
108 return $this;
109 }
110
111 /**
112 * {@inheritDoc}
113 */
114 protected function send(string $content, array $records): void
115 {
116 $contentType = $this->getContentType() ?: ($this->isHtmlBody($content) ? 'text/html' : 'text/plain');
117
118 if ($contentType !== 'text/html') {
119 $content = wordwrap($content, $this->maxColumnWidth);
120 }
121
122 $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
123 $headers .= 'Content-type: ' . $contentType . '; charset=' . $this->getEncoding() . "\r\n";
124 if ($contentType === 'text/html' && false === strpos($headers, 'MIME-Version:')) {
125 $headers .= 'MIME-Version: 1.0' . "\r\n";
126 }
127
128 $subject = $this->subject;
129 if ($records) {
130 $subjectFormatter = new LineFormatter($this->subject);
131 $subject = $subjectFormatter->format($this->getHighestRecord($records));
132 }
133
134 $parameters = implode(' ', $this->parameters);
135 foreach ($this->to as $to) {
136 mail($to, $subject, $content, $headers, $parameters);
137 }
138 }
139
140 public function getContentType(): ?string
141 {
142 return $this->contentType;
143 }
144
145 public function getEncoding(): string
146 {
147 return $this->encoding;
148 }
149
150 /**
151 * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML messages.
152 */
153 public function setContentType(string $contentType): self
154 {
155 if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
156 throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
157 }
158
159 $this->contentType = $contentType;
160
161 return $this;
162 }
163
164 public function setEncoding(string $encoding): self
165 {
166 if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
167 throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
168 }
169
170 $this->encoding = $encoding;
171
172 return $this;
173 }
174 }
175