1、app里面创建Mail文件夹:
里面编写register文件:
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Register extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $subject = '注册邮件';
/**
* Create a new message instance.
* user并非强制的。若自己模拟可以把User限制去掉
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('email.register');
}
}
2、邮件html模板:
在views里面的email里面编写register.blade.php
3、编写mailController
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;
use App\Mail\Register;
class MailController extends Controller
{
public function sendtest(){
//由于我这里模拟用户注册发送邮件,用内置类。
$user = new \stdclass;
$user->name='hello world';
Mail::to('XXX@qq.com')
->queue(new Register($user));
echo 'success';
}
}
4、开启队列
php artisan queue:work
5、配置env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=465
MAIL_USERNAME=name@163.com
MAIL_PASSWORD=password
MAIL_FROM_ADDRESS=name@163.com
MAIL_FROM_NAME=learn(可自定义)
MAIL_ENCRYPTION=ssl