簡體   English   中英

如何將確認郵件發送到 Laravel 中的新 email 地址?

[英]How to send confirmation mail to new email address in Laravel?

我有一個可以更改用戶當前 email 地址的表格。 I want when user changes his current email address to a new email address to automatically sends confirmation mail to this new email address where user would confirm that email. 怎么可能呢? 這是我的代碼。 任何幫助表示贊賞。

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'],
    ];
 }

To laravel 5.7+ 在您的App\Models\UserApp\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();

簡短的回答:

您所要做的就是調用event(new \Illuminate\Auth\Events\Registered($user));

如果這不起作用,您可能需要先設置一些東西(請參閱更長的答案)。

更長的答案:

為了添加對用戶 email 驗證的后端支持,您需要做一些事情。

第一步是確保您的User model 實施正確的方法:

// app/User.php:
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail {
    // this provides:
    //  ->markEmailAsVerified(),
    //  ->hasVerifiedEmail(): bool
    //  ->sendEmailVerificationNotification()
    use \Illuminate\Auth\MustVerifyEmail;

有一個很好的內置事件,您可以在用戶注冊后使用它向用戶觸發 email。 您可以手動觸發它,也可以作為 model 偵聽器的一部分(此處未討論)。 不過,這需要首先在您的事件偵聽器中注冊。 有關更多信息,請參閱Laravel 的事件指南

// app/Providers/EventServiceProvider.php:
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider {

    protected $listen = [
        // ...
        'Illuminate\Auth\Events\Registered' => [
            'Illuminate\Auth\Listeners\SendEmailVerificationNotification',
        ],

完成這兩件事后,可以在應用程序的任何位置觸發驗證 email:

// fire off the Registered event, so that new users get a verification email
event(new \Illuminate\Auth\Events\Registered($user));

這僅在事件偵聽器到位並且User具有正確的層次結構時才有效。

事件處理程序 ( SendEmailVerificationNotification ) 檢查$user是否是尚未驗證的MustVerifyEmail ,如果是,它會調用$user->sendEmailVerificationNotification來觸發\Illuminate\Auth\Notifications\VerifyEmail的實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM