如何在Laravel的app / mail VeryfyMail.php上生成随机令牌?

时间:2019-07-05 13:56:10

标签: php laravel-5

使用Laravel 5.7,我有VeryfyMail系统。我需要在应用程序/邮件VeryfyMail.php文件中的电子邮件上发送一些随机数作为主题,

VeryfyMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class VerifyMail extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }


    public function build()
    {
        //return $this->view('emails.verifyUser');
        return $this->subject('')->view('emails.verifyUser');
    }
}

如何在上述文件主题上生成一些拉面编号?

1 个答案:

答案 0 :(得分:2)

由于您说过要让他将随机令牌作为主题,所以请这样做:

获取随机字符串(十六进制)

创建令牌时,通常需要加密保护的东西,以使聪明的攻击者更难以“猜测”令牌。幸运的是,PHP在PHP 7中引入了random_bytes()

这将创建一个十六进制随机令牌:

// Get some random bytes
$token = random_bytes(8);

// Since random_bytes() returns a string with all kinds of bytes, 
// it can't be presented "as is".
// We need to convert it to a better format. Let's use hex
$token = bin2hex($token)

// Now just add the variable as the subject
return $this->subject($token)->view('emails.verifyUser');

获取随机数(整数)

如果您只希望数字,则可以改用random_int()

// Generate the token. Add the min and max value
$token = random_int(1000000, 9999999);

// Use it as the subject
return $this->subject($token)->view('emails.verifyUser');