How to disable “verify_peer” with Symfony Mailer component?

后端 未结 3 1451
轻奢々
轻奢々 2021-01-11 18:06

I\'m configuring a mail server (postfix), with a self signed certificate, and it seems this self signed certificate is a problem for the Symfony Mailer component.

On

3条回答
  •  青春惊慌失措
    2021-01-11 18:30

    Unfortunatly verify_peer feature is not in symfony 4.4 (yet) as @yivi states correctly.

    I tried updating symfony/mailer in composer to dev-master but symfony flex constraints doesnt allow this due to:

    Restricting packages listed in "symfony/symfony" to "4.4.*"

    So i ended up overriding mailer.transport_factory.smtp:

    mailer.transport_factory.smtp:
        class: App\Mailer\EsmtpTransportFactory
        tags:
          - { name: 'mailer.transport_factory', priority: "-100" }
    

    with a custom EsmtpTransportFactory that contains this feature:

    getScheme() ? true : null;
            $port = $dsn->getPort(0);
            $host = $dsn->getHost();
    
            $transport = new EsmtpTransport($host, $port, $tls, $this->dispatcher, $this->logger);
    
            if (!$dsn->getOption('verify_peer', true)) {
                /** @var SocketStream $stream */
                $stream = $transport->getStream();
                $streamOptions = $stream->getStreamOptions();
    
                $streamOptions['ssl']['verify_peer'] = false;
                $streamOptions['ssl']['verify_peer_name'] = false;
    
                $stream->setStreamOptions($streamOptions);
            }
    
            if ($user = $dsn->getUser()) {
                $transport->setUsername($user);
            }
    
            if ($password = $dsn->getPassword()) {
                $transport->setPassword($password);
            }
    
            return $transport;
        }
    
        protected function getSupportedSchemes(): array
        {
            return ['smtp', 'smtps'];
        }
    }
    

    Note the bool value if verify_peer in the DSN can't be a string.
    This will not work: MAILER_DSN=smtp://foo@default?verify_peer=false
    This will work: MAILER_DSN=smtp://foo@default?verify_peer=0
    or as mentioned in this comment:

    parameters:
      env(verify): 'false'
    
    framework:
      mailer:
      dsn: '%env(MAILER_DSN)%?verify_peer=%env(bool:verify)%'
    

    I guess it would be better if this feature was ported to 4.4 but so long i use this workaround.

提交回复
热议问题