| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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 Symfony\Component\Mailer\Transport;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Mailer\Exception\TransportException;
|
|
|
15 |
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
|
|
|
16 |
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
|
|
|
17 |
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Factory that configures a transport (sendmail or SMTP) based on php.ini settings.
|
|
|
21 |
*
|
|
|
22 |
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
|
|
|
23 |
*/
|
|
|
24 |
final class NativeTransportFactory extends AbstractTransportFactory
|
|
|
25 |
{
|
|
|
26 |
public function create(Dsn $dsn): TransportInterface
|
|
|
27 |
{
|
|
|
28 |
if (!\in_array($dsn->getScheme(), $this->getSupportedSchemes(), true)) {
|
|
|
29 |
throw new UnsupportedSchemeException($dsn, 'native', $this->getSupportedSchemes());
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
if ($sendMailPath = ini_get('sendmail_path')) {
|
|
|
33 |
return new SendmailTransport($sendMailPath, $this->dispatcher, $this->logger);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
|
|
37 |
throw new TransportException('sendmail_path is not configured in php.ini.');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
// Only for windows hosts; at this point non-windows
|
|
|
41 |
// host have already thrown an exception or returned a transport
|
|
|
42 |
$host = ini_get('SMTP');
|
|
|
43 |
$port = (int) ini_get('smtp_port');
|
|
|
44 |
|
|
|
45 |
if (!$host || !$port) {
|
|
|
46 |
throw new TransportException('smtp or smtp_port is not configured in php.ini.');
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$socketStream = new SocketStream();
|
|
|
50 |
$socketStream->setHost($host);
|
|
|
51 |
$socketStream->setPort($port);
|
|
|
52 |
if (465 !== $port) {
|
|
|
53 |
$socketStream->disableTls();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
return new SmtpTransport($socketStream, $this->dispatcher, $this->logger);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
protected function getSupportedSchemes(): array
|
|
|
60 |
{
|
|
|
61 |
return ['native'];
|
|
|
62 |
}
|
|
|
63 |
}
|