SendmailTransport example


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');

        

        yield [
            new Dsn('sendmail+smtp', 'default'),
            true,
        ];
    }

    public static function createProvider(): iterable
    {
        yield [
            new Dsn('sendmail+smtp', 'default'),
            new SendmailTransport(null, null, new NullLogger()),
        ];

        yield [
            new Dsn('sendmail+smtp', 'default', null, null, null, ['command' => '/usr/sbin/sendmail -oi -t']),
            new SendmailTransport('/usr/sbin/sendmail -oi -t', null, new NullLogger()),
        ];
    }

    public static function unsupportedSchemeProvider(): iterable
    {
        yield [
            
'sendmail_path' => $sendmaiPath,
            'smtp' => $smtp,
            'smtp_port' => $smtpPort,
        ];

        $sut = new NativeTransportFactory();
        $sut->create(Dsn::fromString($dsn));
    }

    public static 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);
            
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;

/** * @author Konstantin Myakshin <molodchick@gmail.com> */
final class SendmailTransportFactory extends AbstractTransportFactory
{
    public function create(Dsn $dsn): TransportInterface
    {
        if ('sendmail+smtp' === $dsn->getScheme() || 'sendmail' === $dsn->getScheme()) {
            return new SendmailTransport($dsn->getOption('command')$this->dispatcher, $this->logger);
        }

        throw new UnsupportedSchemeException($dsn, 'sendmail', $this->getSupportedSchemes());
    }

    protected function getSupportedSchemes(): array
    {
        return ['sendmail', 'sendmail+smtp'];
    }
}
protected function tearDown(): void
    {
        if (file_exists($this->argsPath)) {
            @unlink($this->argsPath);
        }
        unset($this->argsPath);
    }

    public function testToString()
    {
        $t = new SendmailTransport();
        $this->assertEquals('smtp://sendmail', (string) $t);
    }

    public function testToIsUsedWhenRecipientsAreNotSet()
    {
        if ('\\' === \DIRECTORY_SEPARATOR) {
            $this->markTestSkipped('Windows does not support shebangs nor non-blocking standard streams');
        }

        $mail = new Email();
        $mail
            
Home | Imprint | This part of the site doesn't use cookies.