メール確認用に送付されるメールをスマートに非同期で送信できるようにする方法のみまとめます。
ユーザー登録時にメール確認を行う方法や、ジョブやキューに関する設定については触れません。
はじめに
ジョブやキューに関する設定については触れません。
それらの設定は、以下を参考に事前に行ってください。
新規で通知クラスを作成します
VerifyEmail
クラスをオーバーライドして、拡張したクラスを作成します。
php artisan make:notification VerifyEmailQueued
自動でメソッドがいろいろ書かれていますが、特にカスタマイズしない場合は全て削除して、以下で置き換えてしまって問題ありません。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Auth\Notifications\VerifyEmail;
class VerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
use Queueable;
}
Userモデルのメソッドを上書きする
sendEmailVerificationNotification
メソッドを追加します。
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
...
public function sendEmailVerificationNotification()
{
$this->notify(new \App\Notifications\VerifyEmailQueued);
}
}
なお、パスワードリセットなどのその他通知メールに対しても、必要であれば個別に同様の対応を行う必要があります。