Method 'to' not found in \Illuminate\Support\Facades\Mail Hey guys,
I am trying to send an Email, but I'm getting this error:
Method 'to' not found in \Illuminate\Support\Facades\Mail
I'm doing this on a laravel command, it's like a cron to send all emails that are queued in a table 'EmailQueue'
I have this on top of the file:
use Illuminate\Support\Facades\Mail;
I was reading docs: https://laravel.com/docs/5.4/mail
They say its Mail::to
But it doesn't work? What am I missing here?
I've got mail.php and .env files configured to send the emails with my gmail account (just for testing for now)
Thanks in advance!
Such a simple thing..but can't find the solution anywhere : /
Hey the facade just give an accessor to the real class. You don't have to include it.
You should have smth like this in your config/app.php
'aliases' = [
//Other Alias
'Mail' => Illuminate\Support\Facades\Mail::class,
//Other Alias
],
It creates an alias for this class \Illuminate\Mail\Mailer;
Which you can call this way
use Mail;
Since it's a facade, just add this to the top of the class:
use Mail;
Or use full namespace when using the facade:
\Mail::send
Hope this help for you !!!
use Illuminate\Contracts\Mail\Mailer;
public function addApplicant(Request $request, Mailer $mailer)
{
$mailer->send('emails.toApplicant', [
'applicant' => $applicant,
'applicationDetailUrl' => $applicationDetailUrl
], function ($m) use ($applicant) {
$m->to($applicant->email, $applicant->name)->subject('Application Submitted Successfully');
});
}
or if you want to use Mail, u can importing same facade but using it as :
$data = [
'key' => 'value'
];
\Mail::send('login', $data, function ($m) use ($data) {
$m->to('[email protected] ', 'rizwan')->subject('Route test!');
});
dd(Mail::failures());
```
@Parasoul @saurabhd I do have that:
'aliases' = [
//Other Alias
'Mail' => Illuminate\Support\Facades\Mail::class,
//Other Alias
],
on my config/app.php
But when I do this "use Mail;" on top of my Laravel Command, it says "Undefined Mail class"
Does this work on a Laravel Command? Btw I'm trying to send a Mailable
@KIEYCH Please follow this link hope this work for you !!!
Try just dumping the autoload composer dump-autoload
Sometimes in fresh applications, some things just dont work for reasons unknown. If your app is new, you can always just create a new app
Turns out phpStorm was giving me this error, but it actually works...silly me trusting phpStorm errors...
Please sign in or create an account to participate in this conversation.