【Laravel】メールアドレスを変更する方法を徹底解説【コピペでOK】
リンクを元にLaravelアプリにメールアドレス変更機能を導入しようとしたら「Connection could not be established with host "localhost:1025": stream_socket_client(): Unable to connect to localhost:1025 (Cannot assign requested address)」というエラーが表示されました。エラー箇所は$email_reset->sendEmailResetNotification($token)です。
実現したいこと
エラーを解決しメールアドレス変更機能を実装したい
発生している問題・エラーメッセージ
Connection could not be established with host "localhost:1025": stream_socket_client(): Unable to connect to localhost:1025 (Cannot assign requested address)
該当のソースコード
ChangeEmailController.php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Models\EmailReset; 6use Illuminate\Http\Request; 7use Illuminate\Support\Facades\DB; 8 9class ChangeEmailController extends Controller 10{ 11 public function sendChangeEmailLink(Request $request) 12 { 13 $new_email = $request->new_email; 14 15 // トークン生成 16 $token = hash_hmac( 17 'sha256', 18 str()->random(40) . $new_email, 19 config('app.key') 20 ); 21 22 // トークンをDBに保存 23 DB::beginTransaction(); 24 try { 25 $param = []; 26 $param['user_id'] = auth()->id(); 27 $param['new_email'] = $new_email; 28 $param['token'] = $token; 29 $email_reset = EmailReset::create($param); 30 31 DB::commit(); 32 33 $email_reset->sendEmailResetNotification($token); 34 35 return redirect('/home')->with('flash_message', '確認メールを送信しました。'); 36 } catch (\Exception $e) { 37 echo ($e->getMessage()); 38 exit; 39 DB::rollback(); 40 return redirect('/home')->with('flash_message', 'メール更新に失敗しました。'); 41 } 42 } 43}
試したこと
mailhogでメールテストしたいのにconnection could not be established with host localhost stream_socket_client()を元にdocker-compose.ymlに下記を追記
docker
1services: 2 mail: 3 image: mailhog/mailhog 4 ports: - 8025:8025
補足情報(FW/ツールのバージョンなど)
PHP 8.1.4
Laravel 9.6.0
Docker 20.10.13
あなたの回答
tips
プレビュー