This might be a good use case for events. Fire the event in your verifyUser method after verification.
how to send an email to admin after verify the user?
I am working with Laravel 5.7 and developing a simple booking system. in my application is running following logic.
- user can book a taxi using booking form 2.user should be logged with email and click the verification link. 3.then after click user verification link an email should send to the admin with user data.
following as user booking and sending to user verification controller,
<?php
namespace App\Http\Controllers;
use App\Mail\VerifyMail;
use App\User;
use App\VerifyUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class UserController extends Controller
{
protected function store(Request $request)
{
$user = new User;
$user->email = $request->input('email');
$user->name = $request->input('name');
$user->address = $request->input('address');
$user->save();
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
Mail::to($user->email)->send(new VerifyMail($user));
return redirect()->back()->with('info','We sent you an activation code. Check your email and click on the link to verify');
}
public function verifyUser($token)
{
$verifyUser = VerifyUser::where('token', $token)->first();
if(isset($verifyUser) ){
$user = $verifyUser->user;
if(!$user->verified) {
$verifyUser->user->verified = 1;
$verifyUser->user->save();
$info = "Your e-mail is verified. You can now login.";
}else{
$info = "Your e-mail is already verified. You can now login.";
}
}else{
return redirect()->back()->with('warning', "Sorry your email cannot be identified.");
}
return redirect()->back()->with('info', $info);
}
}
and email verification link view file
<!DOCTYPE html>
<html>
<head>
<title>Welcome Email</title>
</head>
<body>
<h2>Welcome to the site {{$user['name']}}</h2>
<br/>
Your registered email-id is {{$user['email']}} , Please click on the below link to verify your email account
<br/>
<a href="{{url('user/verify', $user->verifyUser->token)}}">Verify Email</a>
</body>
</html>
but I have not an idea about step 3 to send an admin after verify user verification link. my admin email is like this [email protected] how could I manage this problem?
@AMALMAX - Running the artisan command will generate a new class in your app/Mail directory using the name you've provided. This is what they call a 'Mailable'.
In this UserWasVerifiedMail mailable, you accept the user in the constructor and use it in the build method to render a Mail using a view (which you also need to generate).
example of a UserWasVerifiedMail mailable:
// make sure you add the property as public, otherwise it will not be available in the view
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function build()
{
return $this->subject('New user was verified')->view('mails.user.verified');
}
** After you've created this mailable, make sure to import it in your UserController. **
Now, create a new file in resources/views/mails/user called 'verified.blade.php'.
In this file, you can write up your e-mail in HTML and use the blade helpers and syntax as usual. Since you've made the $user property public, it is accessible in here.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>New user was verified</title>
</head>
<body>
Dear admin, <br> <br>
A new user was verified in your system: <br> <br>
Name of the user: {{ $user->name }} <br>
<!-- etc, etc... -->
</body>
</html>
Does this answer your question?
Please or to participate in this conversation.