Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Revision 399 | 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
 
174
        $transport = $factory->create(new Dsn(
175
            ! empty($config['encryption']) && $config['encryption'] === 'tls' ? (($config['port'] == 465) ? 'smtps' : 'smtp') : '',
176
            $config['host'],
177
            $config['username'] ?? null,
178
            $config['password'] ?? null,
179
            $config['port'] ?? null,
180
            $config
181
        ));
182
 
183
        return $this->configureSmtpTransport($transport, $config);
184
    }
185
 
186
    /**
187
     * Configure the additional SMTP driver options.
188
     *
189
     * @param  \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport  $transport
190
     * @param  array  $config
191
     * @return \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
192
     */
193
    protected function configureSmtpTransport(EsmtpTransport $transport, array $config)
194
    {
195
        $stream = $transport->getStream();
196
 
197
        if ($stream instanceof SocketStream) {
198
            if (isset($config['source_ip'])) {
199
                $stream->setSourceIp($config['source_ip']);
200
            }
201
 
202
            if (isset($config['timeout'])) {
203
                $stream->setTimeout($config['timeout']);
204
            }
205
        }
206
 
207
        return $transport;
208
    }
209
 
210
    /**
211
     * Create an instance of the Symfony Sendmail Transport driver.
212
     *
213
     * @param  array  $config
214
     * @return \Symfony\Component\Mailer\Transport\SendmailTransport
215
     */
216
    protected function createSendmailTransport(array $config)
217
    {
218
        return new SendmailTransport(
219
            $config['path'] ?? $this->app['config']->get('mail.sendmail')
220
        );
221
    }
222
 
223
    /**
224
     * Create an instance of the Symfony Amazon SES Transport driver.
225
     *
226
     * @param  array  $config
150 lars 227
     * @return \Illuminate\Mail\Transport\SesTransport
148 lars 228
     */
229
    protected function createSesTransport(array $config)
230
    {
231
        $config = array_merge(
232
            $this->app['config']->get('services.ses', []),
233
            ['version' => 'latest', 'service' => 'email'],
234
            $config
235
        );
236
 
237
        $config = Arr::except($config, ['transport']);
238
 
239
        return new SesTransport(
240
            new SesClient($this->addSesCredentials($config)),
241
            $config['options'] ?? []
242
        );
243
    }
244
 
245
    /**
246
     * Add the SES credentials to the configuration array.
247
     *
248
     * @param  array  $config
249
     * @return array
250
     */
251
    protected function addSesCredentials(array $config)
252
    {
253
        if (! empty($config['key']) && ! empty($config['secret'])) {
254
            $config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
255
        }
256
 
257
        return Arr::except($config, ['token']);
258
    }
259
 
260
    /**
261
     * Create an instance of the Symfony Mail Transport driver.
262
     *
263
     * @return \Symfony\Component\Mailer\Transport\SendmailTransport
264
     */
265
    protected function createMailTransport()
266
    {
267
        return new SendmailTransport;
268
    }
269
 
270
    /**
271
     * Create an instance of the Symfony Mailgun Transport driver.
272
     *
273
     * @param  array  $config
274
     * @return \Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunApiTransport
275
     */
276
    protected function createMailgunTransport(array $config)
277
    {
150 lars 278
        $factory = new MailgunTransportFactory(null, $this->getHttpClient($config));
148 lars 279
 
280
        if (! isset($config['secret'])) {
281
            $config = $this->app['config']->get('services.mailgun', []);
282
        }
283
 
284
        return $factory->create(new Dsn(
285
            'mailgun+'.($config['scheme'] ?? 'https'),
286
            $config['endpoint'] ?? 'default',
287
            $config['secret'],
288
            $config['domain']
289
        ));
290
    }
291
 
292
    /**
293
     * Create an instance of the Symfony Postmark Transport driver.
294
     *
295
     * @param  array  $config
296
     * @return \Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkApiTransport
297
     */
298
    protected function createPostmarkTransport(array $config)
299
    {
150 lars 300
        $factory = new PostmarkTransportFactory(null, $this->getHttpClient($config));
148 lars 301
 
302
        $options = isset($config['message_stream_id'])
303
                    ? ['message_stream' => $config['message_stream_id']]
304
                    : [];
305
 
306
        return $factory->create(new Dsn(
307
            'postmark+api',
308
            'default',
309
            $config['token'] ?? $this->app['config']->get('services.postmark.token'),
310
            null,
311
            null,
312
            $options
313
        ));
314
    }
315
 
316
    /**
317
     * Create an instance of the Symfony Failover Transport driver.
318
     *
319
     * @param  array  $config
320
     * @return \Symfony\Component\Mailer\Transport\FailoverTransport
321
     */
322
    protected function createFailoverTransport(array $config)
323
    {
324
        $transports = [];
325
 
326
        foreach ($config['mailers'] as $name) {
327
            $config = $this->getConfig($name);
328
 
329
            if (is_null($config)) {
330
                throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
331
            }
332
 
333
            // Now, we will check if the "driver" key exists and if it does we will set
334
            // the transport configuration parameter in order to offer compatibility
335
            // with any Laravel <= 6.x application style mail configuration files.
336
            $transports[] = $this->app['config']['mail.driver']
337
                ? $this->createSymfonyTransport(array_merge($config, ['transport' => $name]))
338
                : $this->createSymfonyTransport($config);
339
        }
340
 
341
        return new FailoverTransport($transports);
342
    }
343
 
344
    /**
345
     * Create an instance of the Log Transport driver.
346
     *
347
     * @param  array  $config
348
     * @return \Illuminate\Mail\Transport\LogTransport
349
     */
350
    protected function createLogTransport(array $config)
351
    {
352
        $logger = $this->app->make(LoggerInterface::class);
353
 
354
        if ($logger instanceof LogManager) {
355
            $logger = $logger->channel(
356
                $config['channel'] ?? $this->app['config']->get('mail.log_channel')
357
            );
358
        }
359
 
360
        return new LogTransport($logger);
361
    }
362
 
363
    /**
364
     * Create an instance of the Array Transport Driver.
365
     *
366
     * @return \Illuminate\Mail\Transport\ArrayTransport
367
     */
368
    protected function createArrayTransport()
369
    {
370
        return new ArrayTransport;
371
    }
372
 
373
    /**
150 lars 374
     * Get a configured Symfony HTTP client instance.
375
     *
376
     * @return \Symfony\Contracts\HttpClient\HttpClientInterface|null
377
     */
378
    protected function getHttpClient(array $config)
379
    {
380
        if ($options = ($config['client'] ?? false)) {
381
            $maxHostConnections = Arr::pull($options, 'max_host_connections', 6);
382
            $maxPendingPushes = Arr::pull($options, 'max_pending_pushes', 50);
383
 
384
            return HttpClient::create($options, $maxHostConnections, $maxPendingPushes);
385
        }
386
    }
387
 
388
    /**
148 lars 389
     * Set a global address on the mailer by type.
390
     *
391
     * @param  \Illuminate\Mail\Mailer  $mailer
392
     * @param  array  $config
393
     * @param  string  $type
394
     * @return void
395
     */
396
    protected function setGlobalAddress($mailer, array $config, string $type)
397
    {
398
        $address = Arr::get($config, $type, $this->app['config']['mail.'.$type]);
399
 
400
        if (is_array($address) && isset($address['address'])) {
401
            $mailer->{'always'.Str::studly($type)}($address['address'], $address['name']);
402
        }
403
    }
404
 
405
    /**
406
     * Get the mail connection configuration.
407
     *
408
     * @param  string  $name
409
     * @return array
410
     */
411
    protected function getConfig(string $name)
412
    {
413
        // Here we will check if the "driver" key exists and if it does we will use
414
        // the entire mail configuration file as the "driver" config in order to
415
        // provide "BC" for any Laravel <= 6.x style mail configuration files.
416
        return $this->app['config']['mail.driver']
417
            ? $this->app['config']['mail']
418
            : $this->app['config']["mail.mailers.{$name}"];
419
    }
420
 
421
    /**
422
     * Get the default mail driver name.
423
     *
424
     * @return string
425
     */
426
    public function getDefaultDriver()
427
    {
428
        // Here we will check if the "driver" key exists and if it does we will use
429
        // that as the default driver in order to provide support for old styles
430
        // of the Laravel mail configuration file for backwards compatibility.
431
        return $this->app['config']['mail.driver'] ??
432
            $this->app['config']['mail.default'];
433
    }
434
 
435
    /**
436
     * Set the default mail driver name.
437
     *
438
     * @param  string  $name
439
     * @return void
440
     */
441
    public function setDefaultDriver(string $name)
442
    {
443
        if ($this->app['config']['mail.driver']) {
444
            $this->app['config']['mail.driver'] = $name;
445
        }
446
 
447
        $this->app['config']['mail.default'] = $name;
448
    }
449
 
450
    /**
451
     * Disconnect the given mailer and remove from local cache.
452
     *
453
     * @param  string|null  $name
454
     * @return void
455
     */
456
    public function purge($name = null)
457
    {
458
        $name = $name ?: $this->getDefaultDriver();
459
 
460
        unset($this->mailers[$name]);
461
    }
462
 
463
    /**
464
     * Register a custom transport creator Closure.
465
     *
466
     * @param  string  $driver
467
     * @param  \Closure  $callback
468
     * @return $this
469
     */
470
    public function extend($driver, Closure $callback)
471
    {
472
        $this->customCreators[$driver] = $callback;
473
 
474
        return $this;
475
    }
476
 
477
    /**
478
     * Get the application instance used by the manager.
479
     *
480
     * @return \Illuminate\Contracts\Foundation\Application
481
     */
482
    public function getApplication()
483
    {
484
        return $this->app;
485
    }
486
 
487
    /**
488
     * Set the application instance used by the manager.
489
     *
490
     * @param  \Illuminate\Contracts\Foundation\Application  $app
491
     * @return $this
492
     */
493
    public function setApplication($app)
494
    {
495
        $this->app = $app;
496
 
497
        return $this;
498
    }
499
 
500
    /**
501
     * Forget all of the resolved mailer instances.
502
     *
503
     * @return $this
504
     */
505
    public function forgetMailers()
506
    {
507
        $this->mailers = [];
508
 
509
        return $this;
510
    }
511
 
512
    /**
513
     * Dynamically call the default driver instance.
514
     *
515
     * @param  string  $method
516
     * @param  array  $parameters
517
     * @return mixed
518
     */
519
    public function __call($method, $parameters)
520
    {
521
        return $this->mailer()->$method(...$parameters);
522
    }
523
}