laravel 修改邮箱提示信息

1.输入 php artisan make:notification RestPassword ,在 app\notification 下创建 RestPassword.php

然后修改 App\User:

复制代码
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword as RestPasswordNotification; // 替换 

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'college', 'class', 'phone'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new RestPasswordNotification($token));
    }
}
复制代码

 2.输入 php artisan vendor:publish --tag=laravel-notifications 

将自动创建 email.blade.php 模版,我这里并不想修改 

3.更改邮件内容,修改第一步创建的 app\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;

    /**
     * The password reset token.
     *
     * @var string
     */
    public $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('XXXX')
                    ->salutation('XXX')
                    ->line('您之所以收到这封邮件是因为我们收到了您重置密码的申请。')
                    ->action('Reset Password', url(config('app.url').route('password.reset', $this->token, false)))
                    ->line('如果您本人未进行密码重置,您可以不必采取进一步操作!');
    }

}
复制代码
Laravel 中进行邮箱验证可以通过以下步骤实现: 1. 在控制器中定义验证规则: ```php $rules = [ 'email' => 'required|email|unique:users,email', ]; ``` 2. 在控制器中引入 Validator 并使用 validate 方法进行验证: ```php use Illuminate\Support\Facades\Validator; $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { return redirect()->back()->withErrors($validator); } // 验证通过,继续执行其他操作 ``` 3. 在表单中添加邮箱输入框并设置 name 属性为 email: ```html <form action="/register" method="POST"> @csrf <label for="email">邮箱</label> <input type="email" name="email" id="email"> <button type="submit">注册</button> </form> ``` 4. 在邮箱验证邮件中添加验证链接并发送给用户: ```php use Illuminate\Support\Facades\Mail; use App\Mail\VerificationEmail; Mail::to($user->email) ->send(new VerificationEmail($user->verification_token)); ``` 5. 创建 VerificationEmail 类并定义邮件内容及验证链接: ```php use Illuminate\Mail\Mailable; class VerificationEmail extends Mailable { public $verificationToken; public function __construct(string $verificationToken) { $this->verificationToken = $verificationToken; } public function build() { $url = route('verify-email', $this->verificationToken); return $this->view('emails.verification', ['url' => $url]); } } ``` 6. 创建邮箱验证路由并处理验证逻辑: ```php use App\Models\User; Route::get('/verify-email/{token}', function ($token) { $user = User::where('verification_token', $token)->first(); if (!$user) { abort(404); } $user->update(['verification_token' => null]); return view('emails.verification-success'); })->name('verify-email'); ``` 以上就是 Laravel 中进行邮箱验证的基本步骤,具体实现可能会有所不同,但大致思路是相似的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值