1 | <?php declare(strict_types=1);
|
---|
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\Level;
|
---|
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 string[]
|
---|
28 | */
|
---|
29 | protected array $to;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * The subject of the email
|
---|
33 | */
|
---|
34 | protected string $subject;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Optional headers for the message
|
---|
38 | * @var string[]
|
---|
39 | */
|
---|
40 | protected array $headers = [];
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Optional parameters for the message
|
---|
44 | * @var string[]
|
---|
45 | */
|
---|
46 | protected array $parameters = [];
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * The wordwrap length for the message
|
---|
50 | */
|
---|
51 | protected int $maxColumnWidth;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * The Content-type for the message
|
---|
55 | */
|
---|
56 | protected string|null $contentType = null;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * The encoding for the message
|
---|
60 | */
|
---|
61 | protected string $encoding = 'utf-8';
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * @param string|string[] $to The receiver of the mail
|
---|
65 | * @param string $subject The subject of the mail
|
---|
66 | * @param string $from The sender of the mail
|
---|
67 | * @param int $maxColumnWidth The maximum column width that the message lines will have
|
---|
68 | */
|
---|
69 | public function __construct(string|array $to, string $subject, string $from, int|string|Level $level = Level::Error, bool $bubble = true, int $maxColumnWidth = 70)
|
---|
70 | {
|
---|
71 | parent::__construct($level, $bubble);
|
---|
72 | $this->to = (array) $to;
|
---|
73 | $this->subject = $subject;
|
---|
74 | $this->addHeader(sprintf('From: %s', $from));
|
---|
75 | $this->maxColumnWidth = $maxColumnWidth;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Add headers to the message
|
---|
80 | *
|
---|
81 | * @param string|string[] $headers Custom added headers
|
---|
82 | * @return $this
|
---|
83 | */
|
---|
84 | public function addHeader($headers): self
|
---|
85 | {
|
---|
86 | foreach ((array) $headers as $header) {
|
---|
87 | if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
|
---|
88 | throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
|
---|
89 | }
|
---|
90 | $this->headers[] = $header;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return $this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Add parameters to the message
|
---|
98 | *
|
---|
99 | * @param string|string[] $parameters Custom added parameters
|
---|
100 | * @return $this
|
---|
101 | */
|
---|
102 | public function addParameter($parameters): self
|
---|
103 | {
|
---|
104 | $this->parameters = array_merge($this->parameters, (array) $parameters);
|
---|
105 |
|
---|
106 | return $this;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * @inheritDoc
|
---|
111 | */
|
---|
112 | protected function send(string $content, array $records): void
|
---|
113 | {
|
---|
114 | $contentType = $this->getContentType() ?? ($this->isHtmlBody($content) ? 'text/html' : 'text/plain');
|
---|
115 |
|
---|
116 | if ($contentType !== 'text/html') {
|
---|
117 | $content = wordwrap($content, $this->maxColumnWidth);
|
---|
118 | }
|
---|
119 |
|
---|
120 | $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
|
---|
121 | $headers .= 'Content-type: ' . $contentType . '; charset=' . $this->getEncoding() . "\r\n";
|
---|
122 | if ($contentType === 'text/html' && false === strpos($headers, 'MIME-Version:')) {
|
---|
123 | $headers .= 'MIME-Version: 1.0' . "\r\n";
|
---|
124 | }
|
---|
125 |
|
---|
126 | $subjectFormatter = new LineFormatter($this->subject);
|
---|
127 | $subject = $subjectFormatter->format($this->getHighestRecord($records));
|
---|
128 |
|
---|
129 | $parameters = implode(' ', $this->parameters);
|
---|
130 | foreach ($this->to as $to) {
|
---|
131 | mail($to, $subject, $content, $headers, $parameters);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public function getContentType(): ?string
|
---|
136 | {
|
---|
137 | return $this->contentType;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public function getEncoding(): string
|
---|
141 | {
|
---|
142 | return $this->encoding;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML messages.
|
---|
147 | * @return $this
|
---|
148 | */
|
---|
149 | public function setContentType(string $contentType): self
|
---|
150 | {
|
---|
151 | if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
|
---|
152 | throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
|
---|
153 | }
|
---|
154 |
|
---|
155 | $this->contentType = $contentType;
|
---|
156 |
|
---|
157 | return $this;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * @return $this
|
---|
162 | */
|
---|
163 | public function setEncoding(string $encoding): self
|
---|
164 | {
|
---|
165 | if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
|
---|
166 | throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
|
---|
167 | }
|
---|
168 |
|
---|
169 | $this->encoding = $encoding;
|
---|
170 |
|
---|
171 | return $this;
|
---|
172 | }
|
---|
173 | }
|
---|