Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Mail;
4
 
5
use Aws\Ses\SesClient;
6
use Closure;
7
use Illuminate\Contracts\Mail\Factory as FactoryContract;
8
use Illuminate\Log\LogManager;
9
use Illuminate\Mail\Transport\ArrayTransport;
10
use Illuminate\Mail\Transport\LogTransport;
11
use Illuminate\Mail\Transport\SesTransport;
12
use Illuminate\Support\Arr;
13
use Illuminate\Support\Str;
14
use InvalidArgumentException;
15
use Psr\Log\LoggerInterface;
150 lars 16
use Symfony\Component\HttpClient\HttpClient;
148 lars 17
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
18
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
19
use Symfony\Component\Mailer\Transport\Dsn;
20
use Symfony\Component\Mailer\Transport\FailoverTransport;
21
use Symfony\Component\Mailer\Transport\SendmailTransport;
22
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
23
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
24
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
25
 
26
/**
27
 * @mixin \Illuminate\Mail\Mailer
28
 */
29
class MailManager implements FactoryContract
30
{
31
    /**
32
     * The application instance.
33
     *
34
     * @var \Illuminate\Contracts\Foundation\Application
35
     */
36
    protected $app;
37
 
38
    /**
39
     * The array of resolved mailers.
40
     *
41
     * @var array
42
     */
43
    protected $mailers = [];
44
 
45
    /**
46
     * The registered custom driver creators.
47
     *
48
     * @var array
49
     */
50
    protected $customCreators = [];
51
 
52
    /**
53
     * Create a new Mail manager instance.
54
     *
55
     * @param  \Illuminate\Contracts\Foundation\Application  $app
56
     * @return void
57
     */
58
    public function __construct($app)
59
    {
60
        $this->app = $app;
61
    }
62
 
63
    /**
64
     * Get a mailer instance by name.
65
     *
66
     * @param  string|null  $name
67
     * @return \Illuminate\Contracts\Mail\Mailer
68
     */
69
    public function mailer($name = null)
70
    {
71
        $name = $name ?: $this->getDefaultDriver();
72
 
73
        return $this->mailers[$name] = $this->get($name);
74
    }
75
 
76
    /**
77
     * Get a mailer driver instance.
78
     *
79
     * @param  string|null  $driver
80
     * @return \Illuminate\Mail\Mailer
81
     */
82
    public function driver($driver = null)
83
    {
84
        return $this->mailer($driver);
85
    }
86
 
87
    /**
88
     * Attempt to get the mailer from the local cache.
89
     *
90
     * @param  string  $name
91
     * @return \Illuminate\Mail\Mailer
92
     */
93
    protected function get($name)
94
    {
95
        return $this->mailers[$name] ?? $this->resolve($name);
96
    }
97
 
98
    /**
99
     * Resolve the given mailer.
100
     *
101
     * @param  string  $name
102
     * @return \Illuminate\Mail\Mailer
103
     *
104
     * @throws \InvalidArgumentException
105
     */
106
    protected function resolve($name)
107
    {
108
        $config = $this->getConfig($name);
109
 
110
        if (is_null($config)) {
111
            throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
112
        }
113
 
114
        // Once we have created the mailer instance we will set a container instance
115
        // on the mailer. This allows us to resolve mailer classes via containers
116
        // for maximum testability on said classes instead of passing Closures.
117
        $mailer = new Mailer(
118
            $name,
119
            $this->app['view'],
120
            $this->createSymfonyTransport($config),
121
            $this->app['events']
122
        );
123
 
124
        if ($this->app->bound('queue')) {
125
            $mailer->setQueue($this->app['queue']);
126
        }
127
 
128
        // Next we will set all of the global addresses on this mailer, which allows
129
        // for easy unification of all "from" addresses as well as easy debugging
130
        // of sent messages since these will be sent to a single email address.
131
        foreach (['from', 'reply_to', 'to', 'return_path'] as $type) {
132
            $this->setGlobalAddress($mailer, $config, $type);
133
        }
134
 
135
        return $mailer;
136
    }
137
 
138
    /**
139
     * Create a new transport instance.
140
     *
141
     * @param  array  $config
142
     * @return \Symfony\Component\Mailer\Transport\TransportInterface
143
     *
144
     * @throws \InvalidArgumentException
145
     */
146
    public function createSymfonyTransport(array $config)
147
    {
148
        // Here we will check if the "transport" key exists and if it doesn't we will
149
        // assume an application is still using the legacy mail configuration file
150
        // format and use the "mail.driver" configuration option instead for BC.
151
        $transport = $config['transport'] ?? $this->app['config']['mail.driver'];
152
 
153
        if (isset($this->customCreators[$transport])) {
154
            return call_user_func($this->customCreators[$transport], $config);
155
        }
156
 
157
        if (trim($transport ?? '') === '' || ! method_exists($this, $method = 'create'.ucfirst($transport).'Transport')) {
158
            throw new InvalidArgumentException("Unsupported mail transport [{$transport}].");
159
        }
160
 
161
        return $this->{$method}($config);
162
    }
163
 
164
    /**
165
     * Create an instance of the Symfony SMTP Transport driver.
166
     *
167
     * @param  array  $config
168
     * @return \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
169
     */
170
    protected function createSmtpTransport(array $config)
171
    {
172
        $factory = new EsmtpTransportFactory;
173
 
399 lars 174
        $scheme = $config['scheme'] ?? null;
175
 
176
        if (! $scheme) {
177
            $scheme = ! empty($config['encryption']) && $config['encryption'] === 'tls'
178
                ? (($config['port'] == 465) ? 'smtps' : 'smtp')
179
                : '';
180
        }
181
 
148 lars 182
        $transport = $factory->create(new Dsn(
399 lars 183
            $scheme,
148 lars 184
            $config['host'],
185
            $config['username'] ?? null,
186
            $config['password'] ?? null,
187
            $config['port'] ?? null,
188
            $config
189
        ));
190
 
191
        return $this->configureSmtpTransport($transport, $config);
192
    }
193
 
194
    /**
195
     * Configure the additional SMTP driver options.
196
     *
197
     * @param  \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport  $transport
198
     * @param  array  $config
199
     * @return \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
200
     */
201
    protected function configureSmtpTransport(EsmtpTransport $transport, array $config)
202
    {
203
        $stream = $transport->getStream();
204
 
205
        if ($stream instanceof SocketStream) {
206
            if (isset($config['source_ip'])) {
207
                $stream->setSourceIp($config['source_ip']);
208
            }
209
 
210
            if (isset($config['timeout'])) {
211
                $stream->setTimeout($config['timeout']);
212
            }
213
        }
214
 
215
        return $transport;
216
    }
217
 
218
    /**
219
     * Create an instance of the Symfony Sendmail Transport driver.
220
     *
221
     * @param  array  $config
222
     * @return \Symfony\Component\Mailer\Transport\SendmailTransport
223
     */
224
    protected function createSendmailTransport(array $config)
225
    {
226
        return new SendmailTransport(
227
            $config['path'] ?? $this->app['config']->get('mail.sendmail')
228
        );
229
    }
230
 
231
    /**
232
     * Create an instance of the Symfony Amazon SES Transport driver.
233
     *
234
     * @param  array  $config
150 lars 235
     * @return \Illuminate\Mail\Transport\SesTransport
148 lars 236
     */
237
    protected function createSesTransport(array $config)
238
    {
239
        $config = array_merge(
240
            $this->app['config']->get('services.ses', []),
241
            ['version' => 'latest', 'service' => 'email'],
242
            $config
243
        );
244
 
245
        $config = Arr::except($config, ['transport']);
246
 
247
        return new SesTransport(
248
            new SesClient($this->addSesCredentials($config)),
249
            $config['options'] ?? []
250
        );
251
    }
252
 
253
    /**
254
     * Add the SES credentials to the configuration array.
255
     *
256
     * @param  array  $config
257
     * @return array
258
     */
259
    protected function addSesCredentials(array $config)
260
    {
261
        if (! empty($config['key']) && ! empty($config['secret'])) {
262
            $config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
263
        }
264
 
265
        return Arr::except($config, ['token']);
266
    }
267
 
268
    /**
269
     * Create an instance of the Symfony Mail Transport driver.
270
     *
271
     * @return \Symfony\Component\Mailer\Transport\SendmailTransport
272
     */
273
    protected function createMailTransport()
274
    {
275
        return new SendmailTransport;
276
    }
277
 
278
    /**
279
     * Create an instance of the Symfony Mailgun Transport driver.
280
     *
281
     * @param  array  $config
399 lars 282
     * @return \Symfony\Component\Mailer\Transport\TransportInterface
148 lars 283
     */
284
    protected function createMailgunTransport(array $config)
285
    {
150 lars 286
        $factory = new MailgunTransportFactory(null, $this->getHttpClient($config));
148 lars 287
 
288
        if (! isset($config['secret'])) {
289
            $config = $this->app['config']->get('services.mailgun', []);
290
        }
291
 
292
        return $factory->create(new Dsn(
293
            'mailgun+'.($config['scheme'] ?? 'https'),
294
            $config['endpoint'] ?? 'default',
295
            $config['secret'],
296
            $config['domain']
297
        ));
298
    }
299
 
300
    /**
301
     * Create an instance of the Symfony Postmark Transport driver.
302
     *
303
     * @param  array  $config
304
     * @return \Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkApiTransport
305
     */
306
    protected function createPostmarkTransport(array $config)
307
    {
150 lars 308
        $factory = new PostmarkTransportFactory(null, $this->getHttpClient($config));
148 lars 309
 
310
        $options = isset($config['message_stream_id'])
311
                    ? ['message_stream' => $config['message_stream_id']]
312
                    : [];
313
 
314
        return $factory->create(new Dsn(
315
            'postmark+api',
316
            'default',
317
            $config['token'] ?? $this->app['config']->get('services.postmark.token'),
318
            null,
319
            null,
320
            $options
321
        ));
322
    }
323
 
324
    /**
325
     * Create an instance of the Symfony Failover Transport driver.
326
     *
327
     * @param  array  $config
328
     * @return \Symfony\Component\Mailer\Transport\FailoverTransport
329
     */
330
    protected function createFailoverTransport(array $config)
331
    {
332
        $transports = [];
333
 
334
        foreach ($config['mailers'] as $name) {
335
            $config = $this->getConfig($name);
336
 
337
            if (is_null($config)) {
338
                throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
339
            }
340
 
341
            // Now, we will check if the "driver" key exists and if it does we will set
342
            // the transport configuration parameter in order to offer compatibility
343
            // with any Laravel <= 6.x application style mail configuration files.
344
            $transports[] = $this->app['config']['mail.driver']
345
                ? $this->createSymfonyTransport(array_merge($config, ['transport' => $name]))
346
                : $this->createSymfonyTransport($config);
347
        }
348
 
349
        return new FailoverTransport($transports);
350
    }
351
 
352
    /**
353
     * Create an instance of the Log Transport driver.
354
     *
355
     * @param  array  $config
356
     * @return \Illuminate\Mail\Transport\LogTransport
357
     */
358
    protected function createLogTransport(array $config)
359
    {
360
        $logger = $this->app->make(LoggerInterface::class);
361
 
362
        if ($logger instanceof LogManager) {
363
            $logger = $logger->channel(
364
                $config['channel'] ?? $this->app['config']->get('mail.log_channel')
365
            );
366
        }
367
 
368
        return new LogTransport($logger);
369
    }
370
 
371
    /**
372
     * Create an instance of the Array Transport Driver.
373
     *
374
     * @return \Illuminate\Mail\Transport\ArrayTransport
375
     */
376
    protected function createArrayTransport()
377
    {
378
        return new ArrayTransport;
379
    }
380
 
381
    /**
150 lars 382
     * Get a configured Symfony HTTP client instance.
383
     *
384
     * @return \Symfony\Contracts\HttpClient\HttpClientInterface|null
385
     */
386
    protected function getHttpClient(array $config)
387
    {
388
        if ($options = ($config['client'] ?? false)) {
389
            $maxHostConnections = Arr::pull($options, 'max_host_connections', 6);
390
            $maxPendingPushes = Arr::pull($options, 'max_pending_pushes', 50);
391
 
392
            return HttpClient::create($options, $maxHostConnections, $maxPendingPushes);
393
        }
394
    }
395
 
396
    /**
148 lars 397
     * Set a global address on the mailer by type.
398
     *
399
     * @param  \Illuminate\Mail\Mailer  $mailer
400
     * @param  array  $config
401
     * @param  string  $type
402
     * @return void
403
     */
404
    protected function setGlobalAddress($mailer, array $config, string $type)
405
    {
406
        $address = Arr::get($config, $type, $this->app['config']['mail.'.$type]);
407
 
408
        if (is_array($address) && isset($address['address'])) {
409
            $mailer->{'always'.Str::studly($type)}($address['address'], $address['name']);
410
        }
411
    }
412
 
413
    /**
414
     * Get the mail connection configuration.
415
     *
416
     * @param  string  $name
417
     * @return array
418
     */
419
    protected function getConfig(string $name)
420
    {
421
        // Here we will check if the "driver" key exists and if it does we will use
422
        // the entire mail configuration file as the "driver" config in order to
423
        // provide "BC" for any Laravel <= 6.x style mail configuration files.
424
        return $this->app['config']['mail.driver']
425
            ? $this->app['config']['mail']
426
            : $this->app['config']["mail.mailers.{$name}"];
427
    }
428
 
429
    /**
430
     * Get the default mail driver name.
431
     *
432
     * @return string
433
     */
434
    public function getDefaultDriver()
435
    {
436
        // Here we will check if the "driver" key exists and if it does we will use
437
        // that as the default driver in order to provide support for old styles
438
        // of the Laravel mail configuration file for backwards compatibility.
439
        return $this->app['config']['mail.driver'] ??
440
            $this->app['config']['mail.default'];
441
    }
442
 
443
    /**
444
     * Set the default mail driver name.
445
     *
446
     * @param  string  $name
447
     * @return void
448
     */
449
    public function setDefaultDriver(string $name)
450
    {
451
        if ($this->app['config']['mail.driver']) {
452
            $this->app['config']['mail.driver'] = $name;
453
        }
454
 
455
        $this->app['config']['mail.default'] = $name;
456
    }
457
 
458
    /**
459
     * Disconnect the given mailer and remove from local cache.
460
     *
461
     * @param  string|null  $name
462
     * @return void
463
     */
464
    public function purge($name = null)
465
    {
466
        $name = $name ?: $this->getDefaultDriver();
467
 
468
        unset($this->mailers[$name]);
469
    }
470
 
471
    /**
472
     * Register a custom transport creator Closure.
473
     *
474
     * @param  string  $driver
475
     * @param  \Closure  $callback
476
     * @return $this
477
     */
478
    public function extend($driver, Closure $callback)
479
    {
480
        $this->customCreators[$driver] = $callback;
481
 
482
        return $this;
483
    }
484
 
485
    /**
486
     * Get the application instance used by the manager.
487
     *
488
     * @return \Illuminate\Contracts\Foundation\Application
489
     */
490
    public function getApplication()
491
    {
492
        return $this->app;
493
    }
494
 
495
    /**
496
     * Set the application instance used by the manager.
497
     *
498
     * @param  \Illuminate\Contracts\Foundation\Application  $app
499
     * @return $this
500
     */
501
    public function setApplication($app)
502
    {
503
        $this->app = $app;
504
 
505
        return $this;
506
    }
507
 
508
    /**
509
     * Forget all of the resolved mailer instances.
510
     *
511
     * @return $this
512
     */
513
    public function forgetMailers()
514
    {
515
        $this->mailers = [];
516
 
517
        return $this;
518
    }
519
 
520
    /**
521
     * Dynamically call the default driver instance.
522
     *
523
     * @param  string  $method
524
     * @param  array  $parameters
525
     * @return mixed
526
     */
527
    public function __call($method, $parameters)
528
    {
529
        return $this->mailer()->$method(...$parameters);
530
    }
531
}