1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BlitzPHP\Mail; |
4
|
|
|
|
5
|
|
|
use BlitzPHP\Mail\Adapters\AbstractAdapter; |
6
|
|
|
use BlitzPHP\Mail\Adapters\NetteMailer; |
|
|
|
|
7
|
|
|
use BlitzPHP\Mail\Adapters\PHPMailer; |
8
|
|
|
use BlitzPHP\Mail\Adapters\SymfonyMailer; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Envoi d'e-mail en utilisant Mail, Sendmail ou SMTP. |
13
|
|
|
* |
14
|
|
|
* @method $this charset(string $charset) |
15
|
|
|
* @method $this priority(int $priority) |
16
|
|
|
* @method $this setCharset(string $charset) |
17
|
|
|
* @method $this setDebug(int $debug = 1) |
18
|
|
|
* @method $this setEncryption(?string $encryption) |
19
|
|
|
* @method $this setHost(string $host) |
20
|
|
|
* @method $this setPassword(string $password); |
21
|
|
|
* @method $this setPort(int $port) |
22
|
|
|
* @method $this setPriority(int $priority) |
23
|
|
|
* @method $this setProtocol(string $protocol) |
24
|
|
|
* @method $this setTimeout(int $timeout) |
25
|
|
|
* @method $this setUsername(string $username) |
26
|
|
|
* @method $this timeout(int $timeout) |
27
|
|
|
*/ |
28
|
|
|
class Mail implements MailerInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Un tableau mappant les schémas d'URL aux noms de classe de moteur d'envoie d'email. |
32
|
|
|
* |
33
|
|
|
* @var array<string, string> |
34
|
|
|
* @psalm-var array<string, class-string> |
35
|
|
|
*/ |
36
|
|
|
protected static array $validHandlers = [ |
37
|
|
|
'phpmailer' => PHPMailer::class, |
38
|
|
|
'symfony' => SymfonyMailer::class, |
39
|
|
|
'nette' => NetteMailer::class, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Configurations |
44
|
|
|
*/ |
45
|
|
|
protected array $config = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Adapter a utiliser pour envoyer les mails |
49
|
|
|
*/ |
50
|
|
|
private ?AbstractAdapter $adapter; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructeur |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $config) |
56
|
|
|
{ |
57
|
|
|
$this->init($config); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
public function init(array $config): self |
64
|
|
|
{ |
65
|
|
|
$this->config = $config; |
66
|
|
|
$this->adapter = null; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function merge(array $config): self |
72
|
|
|
{ |
73
|
|
|
$this->config = array_merge($this->config, $config); |
74
|
|
|
|
75
|
|
|
if (null !== $this->adapter) { |
76
|
|
|
$this->adapter->init($config); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function clear(): void |
83
|
|
|
{ |
84
|
|
|
$this->adapter = null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tente de créer le gestionnaire de mail souhaité |
89
|
|
|
*/ |
90
|
|
|
protected function factory(): AbstractAdapter |
91
|
|
|
{ |
92
|
|
|
if (! empty($this->adapter)) { |
93
|
|
|
return $this->adapter; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$handler = $this->config['handler'] ?? null; |
97
|
|
|
|
98
|
|
|
if (empty($handler)) { |
99
|
|
|
throw new InvalidArgumentException(lang('Mail.undefinedHandler')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (array_key_exists($handler, static::$validHandlers)) { |
103
|
|
|
$handler = static::$validHandlers[$handler]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (! class_exists($handler)) { |
107
|
|
|
throw new InvalidArgumentException(lang('Mail.invalidHandler', [$handler])); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$debug = $this->config['debug'] ?? 'auto'; |
111
|
|
|
if ($debug === 'auto') { |
112
|
|
|
$debug = on_dev(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (! is_subclass_of($handler, AbstractAdapter::class)) { |
116
|
|
|
throw new InvalidArgumentException(lang('Mail.handlerMustExtendClass', [$handler, AbstractAdapter::class])); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @var AbstractAdapter $adapter */ |
120
|
|
|
$adapter = new $handler($debug); |
121
|
|
|
|
122
|
|
|
return $this->adapter = $adapter->init($this->config)->from(...$this->config['from']); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
|
|
public function alt(string $content) : self |
129
|
|
|
{ |
130
|
|
|
$this->factory()->alt($content); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
*/ |
138
|
|
|
public function attach(array|string $path, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): self |
139
|
|
|
{ |
140
|
|
|
$this->factory()->attach($path, $name, $type, $encoding, $disposition); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
|
public function attachBinary($binary, string $name, string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): self |
149
|
|
|
{ |
150
|
|
|
$this->factory()->attachBinary($binary, $name, $type, $encoding, $disposition); |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritDoc} |
157
|
|
|
*/ |
158
|
|
|
public function bcc(array|string $address, bool|string $name = '', bool $set = false) : self |
159
|
|
|
{ |
160
|
|
|
$this->factory()->bcc($address, $name, $set); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritDoc} |
167
|
|
|
*/ |
168
|
|
|
public function cc(array|string $address, bool|string $name = '', bool $set = false): self |
169
|
|
|
{ |
170
|
|
|
$this->factory()->cc($address, $name, $set); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritDoc} |
177
|
|
|
*/ |
178
|
|
|
public function dkim(string $pk, string $passphrase = '', string $selector = '', string $domain = ''): self |
179
|
|
|
{ |
180
|
|
|
$this->factory()->dkim($pk, $passphrase, $selector, $domain); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritDoc} |
187
|
|
|
*/ |
188
|
|
|
public function embedded(string $path, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): self |
189
|
|
|
{ |
190
|
|
|
$this->factory()->embedded($path, $cid, $name, $encoding, $type, $disposition); |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
|
|
public function embeddedBinary($binary, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): self |
199
|
|
|
{ |
200
|
|
|
$this->factory()->embeddedBinary($binary, $cid, $name, $encoding, $type, $disposition); |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritDoc} |
207
|
|
|
*/ |
208
|
|
|
public function from(string $address, string $name = ''): self |
209
|
|
|
{ |
210
|
|
|
$this->factory()->from($address, $name); |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritDoc} |
217
|
|
|
*/ |
218
|
|
|
public function header(array|string $name, ?string $value = null): self |
219
|
|
|
{ |
220
|
|
|
$this->factory()->header($name, $value); |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritDoc} |
227
|
|
|
*/ |
228
|
|
|
public function html(string $content): self |
229
|
|
|
{ |
230
|
|
|
$this->factory()->html($content); |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritDoc} |
237
|
|
|
*/ |
238
|
|
|
public function lastId(): string |
239
|
|
|
{ |
240
|
|
|
return $this->factory()->lastId(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritDoc} |
245
|
|
|
*/ |
246
|
|
|
public function message(string $message): self |
247
|
|
|
{ |
248
|
|
|
return match($this->config['mailType']) { |
249
|
|
|
'html' => $this->html($message), |
250
|
|
|
'text' => $this->text($message), |
251
|
|
|
default => $this |
252
|
|
|
}; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* {@inheritDoc} |
257
|
|
|
*/ |
258
|
|
|
public function replyTo(array|string $address, bool|string $name = '', bool $set = false): self |
259
|
|
|
{ |
260
|
|
|
$this->factory()->replyTo($address, $name, $set); |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* {@inheritDoc} |
267
|
|
|
*/ |
268
|
|
|
public function send() : bool |
269
|
|
|
{ |
270
|
|
|
return $this->factory()->send(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* {@inheritDoc} |
275
|
|
|
*/ |
276
|
|
|
public function sign(string $cert_filename, string $key_filename, string $key_pass, string $extracerts_filename = ''): self |
277
|
|
|
{ |
278
|
|
|
$this->factory()->sign($cert_filename, $key_filename, $key_pass, $extracerts_filename); |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* {@inheritDoc} |
285
|
|
|
*/ |
286
|
|
|
public function subject(string $subject) : self |
287
|
|
|
{ |
288
|
|
|
$this->factory()->subject($subject); |
289
|
|
|
|
290
|
|
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritDoc} |
295
|
|
|
*/ |
296
|
|
|
public function text(string $content): self |
297
|
|
|
{ |
298
|
|
|
$this->factory()->text($content); |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* {@inheritDoc} |
305
|
|
|
*/ |
306
|
|
|
public function to(array|string $address, bool|string $name = '', bool $set = false): self |
307
|
|
|
{ |
308
|
|
|
$this->factory()->to($address, $name, $set); |
309
|
|
|
|
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Utilise une vue html pour generer le message de l'email |
315
|
|
|
*/ |
316
|
|
|
public function view(string $view, array $data = []): self |
317
|
|
|
{ |
318
|
|
|
$path = ''; |
319
|
|
|
|
320
|
|
|
// N'est-il pas namespaced ? on cherche le dossier en fonction du parametre "view_base" |
321
|
|
|
if (strpos($view, '\\') === false) { |
322
|
|
|
$path = $this->config['view_dir'] ?? ''; |
323
|
|
|
if (! empty($path)) { |
324
|
|
|
$path .= '/'; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$view = view($path . $view, $data); |
329
|
|
|
if (! empty($this->config['template'])) { |
330
|
|
|
$view->setLayout($this->config['template']); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $this->html($view->get(false)); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function __call(string $method, array $arguments) |
337
|
|
|
{ |
338
|
|
|
$result = call_user_func_array([$this->factory(), $method], $arguments); |
339
|
|
|
|
340
|
|
|
if ($result instanceof AbstractAdapter) { |
341
|
|
|
return $this; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
return $result; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths