PHP - Swiftmailer using STARTTLS and self signed certificates

前端 未结 4 518
眼角桃花
眼角桃花 2020-12-05 11:32

I\'m trying to send an email with php and swiftmailer, using STARTTLS, but I\'m getting a certificate error. I have root access to the SMTP server, and the certificate used

4条回答
  •  我在风中等你
    2020-12-05 11:58

    For me, I had to add $transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false))); to the Mailer.php file, see:

        /**
         * Returns the SMTP transport
         *
         * @return \Swift_SmtpTransport
         */
        protected function getSmtpInstance(): \Swift_SmtpTransport {
                $transport = new \Swift_SmtpTransport();
                $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10));
                $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1'));
                $transport->setPort($this->config->getSystemValue('mail_smtpport', 25));
                if ($this->config->getSystemValue('mail_smtpauth', false)) {
                        $transport->setUsername($this->config->getSystemValue('mail_smtpname', ''));
                        $transport->setPassword($this->config->getSystemValue('mail_smtppassword', ''));
                        $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN'));
                }
                $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', '');
                if (!empty($smtpSecurity)) {
                        $transport->setEncryption($smtpSecurity);
                }
                $streamingOptions = $this->config->getSystemValue('mail_smtpstreamoptions', []);
                if (is_array($streamingOptions) && !empty($streamingOptions)) {
                        $transport->setStreamOptions($streamingOptions);
                }
    
                /* EDIT - allow self-signed mail cert */
                $transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false)));
                /* EDIT end */
    
                return $transport;
        }
    

    I got this from another link, can't find it now.

    One think that I did extra to the other answers was to specify the 'verify_peer_name' => false

提交回复
热议问题