SmtpTransport example

use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Mime\RawMessage;

/** * @group time-sensitive */
class SmtpTransportTest extends TestCase
{
    public function testToString()
    {
        $t = new SmtpTransport();
        $this->assertEquals('smtps://localhost', (string) $t);

        $t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525)->disableTls());
        $this->assertEquals('smtp://127.0.0.1:2525', (string) $t);
    }

    public function testSendDoesNotPingBelowThreshold()
    {
        $stream = new DummyStream();
        $envelope = new Envelope(new Address('sender@example.org')[new Address('recipient@example.org')]);

        
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'];
    }
}
if (null !== $command) {
            if (!str_contains($command, ' -bs') && !str_contains($command, ' -t')) {
                throw new \InvalidArgumentException(sprintf('Unsupported sendmail command flags "%s"; must be one of "-bs" or "-t" but can include additional flags.', $command));
            }

            $this->command = $command;
        }

        $this->stream = new ProcessStream();
        if (str_contains($this->command, ' -bs')) {
            $this->stream->setCommand($this->command);
            $this->transport = new SmtpTransport($this->stream, $dispatcher$logger);
        }
    }

    public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
    {
        if ($this->transport) {
            return $this->transport->send($message$envelope);
        }

        return parent::send($message$envelope);
    }

    


    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);
            yield ['native://default', '', 'myhost.tld', '465', new SmtpTransport($socketStreamTls)];
        }
    }

    /** * @dataProvider provideCreate */
    
Home | Imprint | This part of the site doesn't use cookies.