発生している問題
laravelで下記サイトを参考にメール送信機能を実装したのですが、
メールが送信されません。
何か足りないものがあればご教示お願いします。
https://qiita.com/yu7a21/items/cc4dc9b9eb78d87f5086
該当のソースコード
Usercontroller.php
1 //3. ユーザデータを保存 2 DB::table('users')->insert($form); 3 4 $now = new DateTime(); 5 $now->format("Y-m-d H:i:s"); 6 //有効期限を計算(30分とした) 7 $expire_at = $now->modify('+30 minutes'); 8 9 $token = new TokenService(); 10 //トークンを生成 11 $token = uniqid('', true); 12 13 $form = [ 14 'token' => $token, 15 'email' => $request->email, 16 'expire_at' => $expire_at, 17 ]; 18 19 DB::table('tokens')->insert($form); 20 21 //4. メールを送信 22 $email = $request->email; 23 24 //メールに記載する認証用URlを組み立てている(認証用ページURL+トークン)。 25 $url = request()->getSchemeAndHttpHost() . "/user/register?token=" . $token; 26 Mail::to($email)->send(new AuthMail($url)); 27 28 //メール送信完了画面へリダイレクト 29 return redirect('/join')->with('email', $email);
AuthMail.php
1<?php 2namespace App; 3 4use Illuminate\Support\Facades\Mail; 5use Illuminate\Bus\Queueable; 6use Illuminate\Mail\Mailable; 7use Illuminate\Queue\SerializesModels; 8 9class AuthMail extends Mailable 10{ 11 use Queueable, SerializesModels; 12 13 protected $url; 14 15 public function __construct($url) 16 { 17 $this->url = $url; 18 } 19 20 //メール送信で使うビュー、タイトル、ビューに渡す認証用URLを設定 21 public function build() 22 { 23 return $this->view('mail.tmpRegist') 24 ->subject('【App】仮登録が完了しました') 25 ->with(['url' => $this->url]); 26 } 27} 28
TokenService.php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6use Illuminate\Contracts\Auth\MustVerifyEmail; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8use Illuminate\Support\Facades\DB; 9use Illuminate\Support\Facades\Log; 10use Illuminate\Notifications\Notifiable; 11use App\Models\SocialUser; 12use App\Token; 13use DateTime; 14use Socialite; 15 16class TokenService extends Model 17{ 18 public function create($email) 19 { 20 $now = new DateTime(); 21 $now->format("Y-m-d H:i:s"); 22 //有効期限を計算(30分とした) 23 $expire_at = $now->modify('+30 minutes'); 24 25 $token = new TokenService(); 26 //トークンを生成 27 $token = uniqid('', true); 28 //3. トークンをDBに保存 29 $token->create([ 30 'token' => $token, 31 'email' => $email, 32 'expire_at' => $expire_at 33 ]); 34 } 35 36 public function matchToken($token) 37 { 38 $now = new DateTime(); 39 40 //6. ユーザから送信されたトークンを検索 41 $data = Token::where([ 42 'token' => $token 43 ])->first(); 44 45 //8. トークンチェック 46 if (is_null($data)) { 47 //DBから値が返ってこないのでトークンが間違っている、チェックNG 48 return "WRONG"; 49 } else if ($data->auth_flag) { 50 //検索して見つかったトークンデータの認証フラグが既に立っている(=認証済み)、チェックNG 51 return "ALREADY"; 52 } 53 54 $expire_date = new DateTime($data->expire_at); 55 56 //9. 認証処理(有効なトークンだった場合はフラグを認証済み(true)に更新) 57 if ($now < $expire_date) { 58 $data->auth_flag = true; 59 $data->update(); 60 return "OK"; 61 } else { 62 //有効期限が切れている、チェックNG 63 //有効期限の切れたトークンデータ、ユーザデータはもう二度と認証できないので削除 64 $email = $data->email; 65 Token::where([ 66 'token' => $token 67 ])->delete(); 68 $userService = new UserService(); 69 $userService->deleteByEmail($email); 70 return "EXPIRE"; 71 } 72 } 73}
補足情報(FW/ツールのバージョンなど)
Laravel Framework 6.20.44
回答1件