如何将确认邮件发送到Laravel中的新电子邮件地址?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将确认邮件发送到Laravel中的新电子邮件地址?相关的知识,希望对你有一定的参考价值。
我有一个可以更改用户当前电子邮件地址的表格。我希望当用户将其当前电子邮件地址更改为新电子邮件地址时,自动将确认邮件发送到该新电子邮件地址,以便用户确认该电子邮件。那怎么办?这是我的代码。任何帮助表示赞赏。
UserProfileController.php
public function changeEmailAddress(UpdateEmailRequest $request)
if ($request->new_email != $request->repeat_email)
return response()->json(['status' => 'Emails do not match.'], 404);
if ($request->new_email == $request->user()->email)
return response()->json(['status' => 'Email cannot be the same as old email'], 404);
if(sizeof(User::where('email','=',$request->new_email)->get()) > 0)
return response()->json(['status' => 'Email already exists'], 404);
$request->user()->email = $request->new_email;
$request->user()->email_verified_at = null;
$request->user()->save();
return response()->json(['status' => 'success'], 200);
index.blade.php
<section data-edit="email" class="editEmail">
<form action=" route('user.update.email') " method="POST" class="flex">
@method('PATCH')
@csrf
<div class="form-group">
<input type="text" placeholder="New Email Adress" name="new_email">
</div>
<div class="form-group">
<input type="text" placeholder="Repeat Email Adress" name="repeat_email">
</div>
<div class="form-group">
<button class="submit">BUTTON</button>
</div>
</form>
</section>
web.php
Route::patch('/profile/change-email', 'UserProfileController@changeEmailAddress')-
>name('user.update.email');
UpdateEmailRequest.php
public function rules()
return [
'email' => ['string', 'email', 'max:255', 'unique:users,email'],
];
答案
至laravel 5.7+在您的App\Models\User
或App\User
中实现MustVerifyEmail
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
和app/routes/web.php
中>
[将电子邮件/验证和电子邮件/重新发送等路由添加到应用程序:
Auth::routes(['verify' => true]);
并且您的方法很简单,只需调用此:
$user->sendEmailVerificationNotification();
以上是关于如何将确认邮件发送到Laravel中的新电子邮件地址?的主要内容,如果未能解决你的问题,请参考以下文章
发送确认链接并单击原始电子邮件后,用新电子邮件更新用户的个人资料