9

I get this error in the controller. I've already added a use Mail statement before the class declaration but still doesn't work.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Redirect,Response,DB,Config; use Mail; class EmailController extends Controller { public function sendEmail() { $user = auth()->user(); Mail::to($user)->send(new MailNotify($user)); if (Mail::failures()) { return response()->Fail('error'); }else{ return response()->success('Successfully send in your mail'); } } } 
4
  • Please have a look- stackoverflow.com/questions/28883894/… Commented Oct 24, 2019 at 17:24
  • thanks. I 've already tried this but it still doesn't work, I get another error. Commented Oct 24, 2019 at 17:58
  • Would you please share the error? Commented Oct 24, 2019 at 18:00
  • error 'Class App\Http\Controllers\MailNotify not found'. I've tried to declare use App\MailNotify;, but I get Class App\MailNotify not found. Commented Oct 24, 2019 at 18:06

1 Answer 1

27

Change this dependency:

use Mail; // ... 

to this one:

use Illuminate\Support\Facades\Mail; // ... 

Also, add this to properly import the other class:

use App\Mail\MailNotify; 

Just then you'll be able to notify your user:

Mail::to($user)->send(new MailNotify($user)); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I've changed it, but I get another error 'Class App\Http\Controllers\MailNotify not found'. I've tried to declare use App\MailNotify;, but I get Class App\MailNotify not found.
That's because this other class is also being wrong imported. When you reference/use a class inside another one, you should reference it using its namespace. Locate where that class is (your IDE should help you with this task) in order to imported it properly.
this use Illuminate\Support\Facades\Mail; saved my day , thank you
Sounds like you need to read up on PHP namespaces and importing classes from namespaces.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.