-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
NativeMailerHandler.php
173 lines (145 loc) · 4.88 KB
/
NativeMailerHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\Level;
use Monolog\Formatter\LineFormatter;
/**
* NativeMailerHandler uses the mail() function to send the emails
*
* @author Christophe Coevoet <[email protected]>
* @author Mark Garrett <[email protected]>
*/
class NativeMailerHandler extends MailHandler
{
/**
* The email addresses to which the message will be sent
* @var string[]
*/
protected array $to;
/**
* The subject of the email
*/
protected string $subject;
/**
* Optional headers for the message
* @var string[]
*/
protected array $headers = [];
/**
* Optional parameters for the message
* @var string[]
*/
protected array $parameters = [];
/**
* The wordwrap length for the message
*/
protected int $maxColumnWidth;
/**
* The Content-type for the message
*/
protected string|null $contentType = null;
/**
* The encoding for the message
*/
protected string $encoding = 'utf-8';
/**
* @param string|string[] $to The receiver of the mail
* @param string $subject The subject of the mail
* @param string $from The sender of the mail
* @param int $maxColumnWidth The maximum column width that the message lines will have
*/
public function __construct(string|array $to, string $subject, string $from, int|string|Level $level = Level::Error, bool $bubble = true, int $maxColumnWidth = 70)
{
parent::__construct($level, $bubble);
$this->to = (array) $to;
$this->subject = $subject;
$this->addHeader(sprintf('From: %s', $from));
$this->maxColumnWidth = $maxColumnWidth;
}
/**
* Add headers to the message
*
* @param string|string[] $headers Custom added headers
* @return $this
*/
public function addHeader($headers): self
{
foreach ((array) $headers as $header) {
if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
}
$this->headers[] = $header;
}
return $this;
}
/**
* Add parameters to the message
*
* @param string|string[] $parameters Custom added parameters
* @return $this
*/
public function addParameter($parameters): self
{
$this->parameters = array_merge($this->parameters, (array) $parameters);
return $this;
}
/**
* @inheritDoc
*/
protected function send(string $content, array $records): void
{
$contentType = $this->getContentType() ?? ($this->isHtmlBody($content) ? 'text/html' : 'text/plain');
if ($contentType !== 'text/html') {
$content = wordwrap($content, $this->maxColumnWidth);
}
$headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n");
$headers .= 'Content-type: ' . $contentType . '; charset=' . $this->getEncoding() . "\r\n";
if ($contentType === 'text/html' && false === strpos($headers, 'MIME-Version:')) {
$headers .= 'MIME-Version: 1.0' . "\r\n";
}
$subjectFormatter = new LineFormatter($this->subject);
$subject = $subjectFormatter->format($this->getHighestRecord($records));
$parameters = implode(' ', $this->parameters);
foreach ($this->to as $to) {
mail($to, $subject, $content, $headers, $parameters);
}
}
public function getContentType(): ?string
{
return $this->contentType;
}
public function getEncoding(): string
{
return $this->encoding;
}
/**
* @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML messages.
* @return $this
*/
public function setContentType(string $contentType): self
{
if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
}
$this->contentType = $contentType;
return $this;
}
/**
* @return $this
*/
public function setEncoding(string $encoding): self
{
if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
}
$this->encoding = $encoding;
return $this;
}
}