-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mailer] Add a transport that uses php.ini settings for configuration #36131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/Symfony/Component/Mailer/Tests/Transport/NativeTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Mailer\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Mailer\Transport\Dsn; | ||
use Symfony\Component\Mailer\Transport\NativeTransportFactory; | ||
use Symfony\Component\Mailer\Transport\SendmailTransport; | ||
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport; | ||
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream; | ||
use Symfony\Component\Mailer\Transport\TransportInterface; | ||
|
||
final class NativeTransportFactoryTest extends TestCase | ||
{ | ||
public static $fakeConfiguration = []; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
$namespace = str_replace('\\Tests\\', '\\', __NAMESPACE__); | ||
|
||
$current = static::class; | ||
|
||
$eval = <<<EOT | ||
namespace $namespace; | ||
|
||
function ini_get(\$key) | ||
{ | ||
\$vals = \\$current::\$fakeConfiguration; | ||
return \$vals[\$key] ?? ''; | ||
} | ||
EOT; | ||
eval($eval); | ||
} | ||
|
||
public function testCreateWithNotSupportedScheme() | ||
{ | ||
$this->expectException(UnsupportedSchemeException::class); | ||
$this->expectExceptionMessageRegExp('#The ".*" scheme is not supported#'); | ||
|
||
$sut = new NativeTransportFactory(); | ||
$sut->create(Dsn::fromString('sendmail://default')); | ||
} | ||
|
||
public function testCreateSendmailWithNoSendmailPath() | ||
{ | ||
if ('\\' === \DIRECTORY_SEPARATOR) { | ||
$this->markTestSkipped('This test cannot run on Windows.'); | ||
} | ||
|
||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage('sendmail_path is not configured'); | ||
|
||
$sut = new NativeTransportFactory(); | ||
$sut->create(Dsn::fromString('native://default')); | ||
} | ||
|
||
public function provideCreateSendmailWithNoHostOrNoPort(): \Generator | ||
{ | ||
yield ['native://default', '', '', '']; | ||
yield ['native://default', '', 'localhost', '']; | ||
yield ['native://default', '', '', '25']; | ||
} | ||
|
||
/** | ||
* @dataProvider provideCreateSendmailWithNoHostOrNoPort | ||
*/ | ||
public function testCreateSendmailWithNoHostOrNoPort(string $dsn, string $sendmaiPath, string $smtp, string $smtpPort) | ||
{ | ||
if ('\\' !== \DIRECTORY_SEPARATOR) { | ||
$this->markTestSkipped('This test only run on Windows.'); | ||
} | ||
|
||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage('smtp or smtp_port is not configured'); | ||
|
||
self::$fakeConfiguration = [ | ||
'sendmail_path' => $sendmaiPath, | ||
'smtp' => $smtp, | ||
'smtp_port' => $smtpPort, | ||
]; | ||
|
||
$sut = new NativeTransportFactory(); | ||
$sut->create(Dsn::fromString($dsn)); | ||
} | ||
|
||
public function provideCreate(): \Generator | ||
{ | ||
yield ['native://default', '/usr/sbin/sendmail -t -i', '', '', new SendmailTransport('/usr/sbin/sendmail -t -i')]; | ||
|
||
if ('\\' === \DIRECTORY_SEPARATOR) { | ||
$socketStream = new SocketStream(); | ||
$socketStream->setHost('myhost.tld'); | ||
$socketStream->setPort(25); | ||
$socketStream->disableTls(); | ||
yield ['native://default', '', 'myhost.tld', '25', new SmtpTransport($socketStream)]; | ||
|
||
$socketStreamTls = new SocketStream(); | ||
$socketStreamTls->setHost('myhost.tld'); | ||
$socketStreamTls->setPort(465); | ||
yield ['native://default', '', 'myhost.tld', '465', new SmtpTransport($socketStreamTls)]; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider provideCreate | ||
*/ | ||
public function testCreate(string $dsn, string $sendmailPath, string $smtp, string $smtpPort, TransportInterface $expectedTransport) | ||
{ | ||
self::$fakeConfiguration = [ | ||
'sendmail_path' => $sendmailPath, | ||
'smtp' => $smtp, | ||
'smtp_port' => $smtpPort, | ||
]; | ||
|
||
$sut = new NativeTransportFactory(); | ||
$transport = $sut->create(Dsn::fromString($dsn)); | ||
|
||
$this->assertEquals($expectedTransport, $transport); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Symfony/Component/Mailer/Transport/NativeTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Transport; | ||
|
||
use Symfony\Component\Mailer\Exception\TransportException; | ||
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport; | ||
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream; | ||
|
||
/** | ||
* Factory that configures a transport (sendmail or SMTP) based on php.ini settings. | ||
* | ||
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com> | ||
*/ | ||
final class NativeTransportFactory extends AbstractTransportFactory | ||
{ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
if (!\in_array($dsn->getScheme(), $this->getSupportedSchemes(), true)) { | ||
throw new UnsupportedSchemeException($dsn, 'native', $this->getSupportedSchemes()); | ||
} | ||
|
||
if ($sendMailPath = ini_get('sendmail_path')) { | ||
return new SendmailTransport($sendMailPath, $this->dispatcher, $this->logger); | ||
} | ||
|
||
if ('\\' !== \DIRECTORY_SEPARATOR) { | ||
throw new TransportException('sendmail_path is not configured in php.ini.'); | ||
} | ||
|
||
// Only for windows hosts; at this point non-windows | ||
// host have already thrown an exception or returned a transport | ||
$host = ini_get('smtp'); | ||
$port = (int) ini_get('smtp_port'); | ||
|
||
if (!$host || !$port) { | ||
throw new TransportException('smtp or smtp_port is not configured in php.ini.'); | ||
} | ||
|
||
$socketStream = new SocketStream(); | ||
$socketStream->setHost($host); | ||
$socketStream->setPort($port); | ||
if (465 !== $port) { | ||
$socketStream->disableTls(); | ||
} | ||
|
||
return new SmtpTransport($socketStream, $this->dispatcher, $this->logger); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['native']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.