PHP Fatal error: Uncaught Swift_TransportException: Connection could not be established with host ssl://smtp.gmail.com :stream_socket_client()

PHP Fatal error: Uncaught Swift_TransportException: Connection could not be established with host ssl://smtp.gmail.com :stream_socket_client()

我正在使用 Windows 10 和 cmd

当我开始我的 index.php 文件时,使用 php index.php 我收到这个错误:

PHP Fatal error:  Uncaught Swift_TransportException: Connection could not be established with host ssl://smtp.gmail.com :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php:269 Stack trace:
#0 [internal function]: Swift_Transport_StreamBuffer->{closure}(2, 'stream_socket_c...', 'C:\Users\barte\...', 272, Array)
#1 C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php(272): stream_socket_client('ssl://smtp.gmai...', 0, '', 30, 4, Resource id
#98)
#2 C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php(58): Swift_Transport_StreamBuffer->establishSocketConnection()
#3 C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php(143): Swift_Transport_Strea in C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php on line 269

Fatal error: Uncaught Swift_TransportException: Connection could not be established with host ssl://smtp.gmail.com :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php:269 Stack trace:
#0 [internal function]: Swift_Transport_StreamBuffer->{closure}(2, 'stream_socket_c...', 'C:\Users\barte\...', 272, Array)
#1 C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php(272): stream_socket_client('ssl://smtp.gmai...', 0, '', 30, 4, Resource id
#98)
#2 C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php(58): Swift_Transport_StreamBuffer->establishSocketConnection()
#3 C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php(143): Swift_Transport_Strea in C:\Users\barte\Desktop\PHP\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php on line 269

我的index.php文件:

<?php

require "vendor/autoload.php";

$transport = new Swift_SmtpTransport("ssl://smtp.gmail.com", 465);
$transport->setUsername("mymail");
$transport->setPassword("******");

$mailer = new Swift_Mailer($transport);

$message = new Swift_Message();
$message
    ->setFrom("mymail@gmail.com")
    ->setTo("mymail@gmail.com")
    ->setSubject("Hello!")
    ->setBody("Hello, this is example body");

$mailer->send($message);

这是我的 composer.json 文件:

{
    "name": "bartek/3",
    "authors": [
        {
            "name": "Bartek",
            "email": "mymail@gmail.com"
        }
    ],
    "require": {
        "swiftmailer/swiftmailer": "^6.2"
    }
}


当我输入 composer --version 我得到:

PHP temp directory (C:\Users\barte\AppData\Local\Temp) does not exist or is not writable to Composer. Set sys_temp_dir in your php.ini Composer version 1.10.5 2020-04-10 11:44:22

谢谢

ssl://smtp.gmail.com 不是邮件服务器的正确地址。如果您跳过协议并仅使用 smtp.gmail.com 会怎样?

我最近为此准备了代码 对于 TLS

$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587,'tls'))
->setUsername('username@gmail.com')
->setPassword('password');

对于SSL

$transport = (new Swift_SmtpTransport('smtp.gmail.com', 25,'ssl'))
->setUsername('username@gmail.com')
->setPassword('password');

我解决了。问题是,我输入了 Google 帐户的密码,但我应该将在 google 帐户设置的“应用程序密码”中生成的密码粘贴到那里。您可以通过转到 Google Account Settings > Security > App passwords

来生成这样的密码

注意:您必须先启用两步验证。否则App passwords不会出现