Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kfiroj236's avatar

How I can verify a user email through his profile , like we show his email address in anchor tag when it click on that anchor tag a verification mail go to him to verify his or her email.

this is my view page layout to show email address in anchor tag but its not working.

 <tr>
           <th scope="row">Email Id</th>
                 <td><a href="{{ route('emailVerify') }}" onclick="event.preventDefault();
                                                                    document.getElementById('emailVerify').submit();">{{Auth::user()->email }}</a></td>

                       <form id="emailVerify" action="{{ route('emailVerify') }}" method="POST">
                            @csrf
                                                                    
                       </form>
                                                                 
</tr>

and this is my route

//email verification
    Route::post('email-verification','UserController@emailVerify')->name('emailVerify');

please help thank you.

0 likes
32 replies
Snapey's avatar

This is only for the currently logged in user to perform themselves?

create a notification class

in your emailVerify method, send the notification along with a token

write a controller method to receive the token and validate the user

There are loads of articles around regarding validation of email address

kfiroj236's avatar

@Snapey thank you for suggestions but problem is this my anchor tag is not working . this is my controller method

public function emailVerify(Request $request){
        dd($request->all());
        Mail::send('email.verify',$confirmation_code,function($message){
            $message->to(Auth::user()->email,Auth::user()->first_name)
            ->subject('Verify you eamil address');
        });
        return redirect()->back();

    }
kfiroj236's avatar

@Snapey I saw the articles but email verification is done at registration time and I want to do this by going his profile page and I give one link to verify email address.

Snapey's avatar

So, as you have been around here a little while, 'not working' is the least favourite problem

Noone can help you with 'not working'

  • When you click the link, does anything happen?
  • Does it hit your controller method?
  • Are any laravel errors displayed?
  • Have you opened the browser development tools and looked for console errors?
  • Have you looked in the browser dev network tab to see if the form is being submitted?
kfiroj236's avatar

@Snapey when I click the link nothing will happen If I used this link like this

<tr>
           <th scope="row">Email Id</th>
                 <td><a href="{{ route('emailVerify') }}">{{Auth::user()->email }}</a></td>
</tr>

It gives me a error like

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

Then I tried like this

<tr>
           <th scope="row">Email Id</th>
                 <td><a href="{{ route('emailVerify') }}" onclick="event.preventDefault();
                                                                    document.getElementById('emailVerify').submit();">{{Auth::user()->email }}</a></td>

                       <form id="emailVerify" action="{{ route('emailVerify') }}" method="POST">
                            @csrf
                                                                    
                       </form>
                                                                 
</tr>

than link is stop working.

Cronix's avatar

MethodNotAllowedHttpException means you sent the wrong kind of request to a route. Like you are sending a POST request to a GET route.

Anchors perform GET requests, so your route method for the emailVerify route needs to be GET.

kfiroj236's avatar

@Cronix ya I know but for sending an email verification link to that particular email we need to post method, so how it will work.

rin4ik's avatar

or with your second option if it is stop working dd please in UserController emailVerify method. does it hit the controller?

Snapey's avatar

Anchors perform GET requests, so your route method for the emailVerify route needs to be GET.

if you are using the <a link on its own, or POST if you are using it to trigger the form submission.

kfiroj236's avatar

@rin4ik ya I check by dd but it does not work it doesn't hit my controller emailVerify .

Snapey's avatar

ya I know but for sending an email verification link to that particular email we need to post method, so how it will work.

what makes you think it must be POST method?

Cronix's avatar

Use a form or use ajax if you must use POST.

Not sure why it needs to be a post request. Wouldn't just sending it to /emailverify/userid work?

Route::get('emailverify/{id}', 'yourController@method');

then grab the user, get their email address, send the email, and redirect back()?

kfiroj236's avatar

@Snapey ya I used with form submission but it not work .

<td><a href="{{ route('emailVerify') }}" onclick="event.preventDefault();
                                                                    document.getElementById('emailVerify').submit();">{{Auth::user()->email }}</a></td>

                       <form id="emailVerify" action="{{ route('emailVerify') }}" method="POST">
                            @csrf
                                                                    
                       </form>
kfiroj236's avatar

this is my view page looks like

Dashboard
You are logged in! 
[email protected]

when I have to verify my email by just clicking on this email how it will be done.

Snapey's avatar

You said...

Thats a javascript issue.

You need to follow the questions I asked earlier.

but as pointed out, you don't have to do it this way. It seems better but complicates it.

kfiroj236's avatar

@Snapey @Cronix This is my route

Route::post('emailverify','HomeController@emailVerify')->name('emailVerify');

and this is my controller method

public function emailVerify(Request $request)
    {
      Mail::send('email.verify', $confirmation_code, function($message) {
            $message->to(Auth::user()->email, Auth::user()->name)
                ->subject('Verify your email address');
        });
        return redirect()->back();
    }
Snapey's avatar

You've posted this before. It won't help us get past 'not working' if you are unable to debug the situation.

Again

  • When you click the link, does anything happen?
  • Does it hit your controller method?
  • Are any laravel errors displayed?
  • Have you opened the browser development tools and looked for console errors?
  • Have you looked in the browser dev network tab to see if the form is being submitted?
Cronix's avatar
Route::get('emailverify','HomeController@emailVerify')->name('emailVerify');

Leave the controller alone, it should be fine as is. You're not even using Request, you're just grabbing the values of the currently logged in user, so it really doesn't NEED to be a POST request because you're not actually sending anything.

Get rid of the javascript and the form in the view.

All you need is:

<a href="{{ route('emailVerify') }}">{{Auth::user()->email }}</a>

Edit: where does $confirmation_code come from? It's not defined.

kfiroj236's avatar

@Cronix ya I did this

<a href="{{ route('emailVerify') }}">{{ Auth::user()->email}}</a>

I changed the route with get method and it will go my controller method through route

Route::get('emailverify','HomeController@emailVerify')->name('emailVerify');

and this is my controller method

public function emailVerify(Request $request)
    {
      Mail::send('email.verify', $confirmation_code, function($message) {
            $message->to(Auth::user()->email, Auth::user()->name)
                ->subject('Verify your email address');
        });
        return redirect()->back();
    }

here it shwoing me error like

Undefined variable: confirmation_code
Cronix's avatar

Yes, I asked where that value comes from. It's not defined in your controller but you use it. That's your code, not something we added.

kfiroj236's avatar

@Cronix I got you point I changed every thing like from my view page

<a href="{{ route('emailVerify'.Auth::user()->$confirmation_code) }}">{{ Auth::user()->email}}</a>

my route

Route::get('emailverify/{id}','HomeController@emailVerify')->name('emailVerify');

and my controller method

public function emailVerify($id,Request $request)
    {
      $confirmation_code=$id;

      Mail::send('email.verify', $confirmation_code, function($message) {
            $message->to(Auth::user()->email, Auth::user()->name)
                ->subject('Verify your email address');
        });
        return redirect()->back();
    }

then it give me error like

Sorry, the page you are looking for could not be found.
Cronix's avatar

You're just trying things quickly and replying back here with your problems. Try spending some time on it and researching before just posting back.

You don't need to send confirmation code in the route. Again, you're just grabbing it off of the currently authenticated user just like you are Auth::user()->email and Auth::user()->name so you don't need to pass it at all. Just use Auth::user()->confirmation_code in the controller.

Please take some time to understand what you are doing or you won't learn anything. This is part of being a developer.

kfiroj236's avatar

@Cronin ok Sure I will be going through all your suggestions and all other friends too who give me some help and then I will come back again with a solution.

Cronix's avatar

Hint: the 2nd parameter to the Mail function should be an array of data to send to the view, not just a variable (unless the variable is an array), just like sending data to any other view.

kfiroj236's avatar

@Cronix @Snapey I am getting error in view page like

Undefined variable: confirmation_code (View: C:\xampp\htdocs\newproject\resources\views\email\verify.blade.php)

and this is my controller

public function emailVerify($id,Request $request){
       $confirmation_code= $id;
        Mail::send('email.verify',compact('$confirmation_code'),function($message){
            $message->to(Auth::user()->email,Auth::user()->first_name)
            ->subject('Verify you eamil address');
        });
        return redirect()->back()->with('info','Please check your email for verification');

    }
Snapey's avatar

you don't need the $ in the compact statement

compact('$confirmation_code')

should be just compact('confirmation_code')

Confirmation code should be a large random number,is it?

kfiroj236's avatar

@Snapey Ya I solve this problem now it will work fine but when I click on link which get in my mail address

Verify Your Email Address
Thanks for creating an account with the verification demo app. Please follow the link below to verify your email address http://localhost:8000/register/verify/sUjTc860AhWWWEgUjQako3haOUgt8ueLp4cZXDMj.

it gives me error

Sorry, the page you are looking for could not be found.
Snapey's avatar

What should it be?

is the final period . part of the token?

What do your other urls look like?

have you setup the verification route in web.php?

Next

Please or to participate in this conversation.