laravel 163发送邮件设置及常见错误

配置163邮箱账户

首先需要有163邮箱,这里在163邮箱必须在设置里面开启SMTP服务,并设置密码

修改laravel根目录下的.env文件, 设置邮箱相关内容:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=465   
MAIL_USERNAME=YOUR-EMAIL-NAME
MAIL_PASSWORD=YOUR-163-PASSWORD    //密码是你设置SMTP设置的密码,不是登录密码
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=YOUR-EMAIL
MAIL_FROM_NAME=YOUR-NAME

注意上面,如果你的网站没有开启ssl,需要设置端口MAIL_PORT为25,同时设置MAIL_ENCRYPTION=null
修改config文件夹下mail.php

'from' => [
        'address' => env('MAIL_FROM_ADDRESS', YOUR-EMAIL'),
        'name' => env('MAIL_FROM_NAME', 'YOUR-NAME'),
    ],

测试发送邮件功能

  1. 创建一个发送邮件测试类:
    需要切换到laravel根目录,执行以下命令
php artisan make:mail TestMail

该命令会在app/Mail目录中创建一个TestMail.php文件,修改可邮寄类 TestMail 的 build 方法如下:

public function build()
{
    return $this->subject('测试邮件')->view('emails.test');
}

上面使用了视图,我们需要在resources/views目录下创建emails目录,在emails目录下创建test.blade.php文件,内容如下:

一封来自laravel测试网站的测试邮件!
  1. 使用Tinker测试发送邮件功能:

错误

  1. 网站没有开启ssl
PHP Warning:  stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in /www/wwwroot/abc/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 267
PHP Warning:  stream_socket_client(): Failed to enable crypto in /www/wwwroot/abc/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 267
PHP Warning:  stream_socket_client(): unable to connect to ssl://smtp.163.com:25 (Unknown error) in /www/wwwroot/abc/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 267

Swift_TransportException with message 'Connection could not be established with host smtp.163.com [ #0]'

当你的网站没有设置ssl,但是配置了MAIL_ENCRYPTION=ssl,就会出现上述错误

  1. 端口设置错误
    端口设置错误信息:
PHP Warning:  stream_socket_client(): unable to connect to smtp.163.com:225 (Connection refused) in /www/wwwroot/abc/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 267
Swift_TransportException with message 'Connection could not be established with host smtp.163.com [Connection refused #111]'


发送邮箱的端口设置参考:http://help.163.com/10/0731/11/6CTUBPT300753VB8.html

参考:

https://laravelacademy.org/post/9743.html
http://help.163.com/10/0731/11/6CTUBPT300753VB8.html

posted @ 2019-07-28 11:32  星空天宇  阅读(326)  评论(0编辑  收藏  举报