1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel6のAuthでパスワードリセットメールをテキストで送る

Posted at

いろいろ調べても情報が見つからず、うまく行った方法をメモ。

1.オリジナルのNotificationを作成

$ php artisan make:notification PasswordResetUserNotification

2.Userモデルで1のオリジナルNotificationを呼び出すようにする。

(CanResetPasswordトレイトのsendPasswordResetNotificationメソッドをUserモデルでオーバーライドする)

app/User.php
<?php

namespace App;

use App\Notifications\PasswordResetUserNotification;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

   
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
	
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new PasswordResetUserNotification($token));    
    }	
}

3.テキストメールを送る Mailableクラスを生成

$ php artisan make:mail ResetPasswordMail
app/Mail/ResetPasswordMail.php
<?php

namespace App\Mail;

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

class ResetPasswordMail extends Mailable
{
    use Queueable, SerializesModels;
	
	public $subject = 'パスワード変更のご案内';	
	
	public function __construct($user,$url)
	{
		$this->user = $user;
		$this->url = $url;
	}

	public function build()
	{
		$result =  $this->view('emails.reset_password',[
			'user'=>$this->user,
			'url'=>$this->url
		]);
		return $result;
	}
}

4.テキストメールのテンプレートを作成

resources/views/emails/reset_password.blade.php
{{ $user->name }} 
<br>
パスワード変更のご案内<br>
次のURLよりパスワードの設定を行ってログインをお願いいたします<br>
<br>
パスワード設定URL<br>
{{ $url }}
<br>
-----------------------
ホゲホゲ株式会社

5.PasswordResetUserNotificationをテキストメールを飛ばすように編集

  • Illuminate\Auth\Notifications\ResetPasswordを継承してtoMailのみをオーバーライドします。
  • toMailでは、toを指定済のMailableオブジェクトをReturnすればOK
<?php

namespace App\Notifications;

use App\Mail\ResetPasswordMail;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Mail;

class PasswordResetUserNotification extends ResetPassword
{
	public function toMail($user)
	{
		if (static::$toMailCallback) {
			return call_user_func(static::$toMailCallback, $notifiable, $this->token);
		}
		$url = url(route('password.reset',['token' => $this->token,'email' => $user->email],false));
		$mail = new ResetPasswordMail($user,$url);
		return $mail->to($user->email);
	}
}

la

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?