1、执行命令 php artisan make:notification RestPassword
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ResetPassword extends Notification
{
use Queueable;
var $token;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('密码重置')
->line('我们收到你的邮件是因为你发了一个重置密码的请求.')
->action('点击重置密码', url('/password/reset',$this->token))
->line('谢谢你的使用!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
复制代码
2、修改 App\User:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','sex','mobile','uid','provider','avatar',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* 发送密码重置通知.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
}
复制代码
3、配置env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=邮箱地址
MAIL_PASSWORD=邮箱密码
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME=发件人复制代码
4、自定义邮件内容
@isset($actionText)
@component('mail::subcopy')
@lang(
"如果你无法点击此按钮 \":actionText\" , 请复制此链接从浏览器中打开: [:actionURL](:actionURL)",
[
'actionText' => $actionText,
'actionURL' => $actionUrl
]
)
@endcomponent
@endisset
@endcomponent复制代码