Laravel 发送邮件遇到的问题

环境 centos 7

php 7.3.9

Connection could not be established with host smtp.exmail.qq.com


参考连接:https://learnku.com/articles/24554

这个问题出现主要原因是一处代码无法连接 smtp.exmail.qq.com

我使用服务器连接发送邮件的服务器也能连接成功

出错代码如下:

<?php
$streamContext = stream_context_create($options);
        $this->stream = @stream_socket_client($host.':'.$this->params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
        if (false === $this->stream) {
            throw new Swift_TransportException(
                'Connection could not be established with host '.$this->params['host'].
                ' ['.$errstr.' #'.$errno.']'
                );
        }

拿出其中的代码测试如下:

<?php
$a=stream_socket_client("ssl://smtp.exmail.qq.com:465", $errno, $errstr, 120, STREAM_CLIENT_CONNECT);

运行结果:

PHP Warning:  stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /web/webdata/x-ac.cn/test.php on line 3

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /web/webdata/x-ac.cn/test.php on line 3
PHP Warning:  stream_socket_client(): Failed to enable crypto in /web/webdata/x-ac.cn/test.php on line 3

Warning: stream_socket_client(): Failed to enable crypto in /web/webdata/x-ac.cn/test.php on line 3
PHP Warning:  stream_socket_client(): unable to connect to ssl://smtp.exmail.qq.com:465 (Unknown error) in /web/webdata/x-ac.cn/test.php on line 3

Warning: stream_socket_client(): unable to connect to ssl://smtp.exmail.qq.com:465 (Unknown error) in /web/webdata/x-ac.cn/test.php on line 3

应该是证书验证失败,现在有两种方法,一种添加证书位置,一种是让PHP不验证证书。

个人推荐添加证书位置

我的证书位置是: /etc/pki/tls/certs/ca-bundle.crt 添加到 php.ini 中即可

curl.cainfo=/etc/pki/tls/certs/ca-bundle.crt
openssl.cafile=/etc/pki/tls/certs/ca-bundle.crt

让PHP不验证证书

修改配置文件config/mail.php

 'password' => env('MAIL_PASSWORD'), // 这段代码无关,只是作为一个参考

    /*
    |--------------------------------------------------------------------------
    | SMTP Server stream context options
    |--------------------------------------------------------------------------
    |
    | This is an array that determines the stream context options and parameters.
    |
    */
    'stream'    => [
        'ssl' => [
            'verify_peer' => false
        ],
    ],

至此邮件发送成功了。


Expected response code 250 but got code "501", with message "501 mail from address must be same as authorization user "

参考资料:https://www.cnblogs.com/jianqingwang/p/6610032.html

大概意思就是 发送邮件地址必须是登录邮件地址。

意思很明显了,修改来自邮件配置即可。

修改 .env

MAIL_FROM_ADDRESS=123@qq.com
MAIL_USERNAME=123@qq.com

邮件可以正常发送了。

阿杰博客
请先登录后发表评论
  • latest comments
  • 总共0条评论