Home » Php » php – Mail::send() not working in Laravel 5

php – Mail::send() not working in Laravel 5

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am continuously getting this error ‘Class ‘App\Http\Controllers\Mail’ not found’ error in my UserController.php

    public function store(CreateUserRequest $request)
{
    $result = DB::table('clients')->select('client_code','name','email')
                                  ->where('client_code','=',$request->code)
                                  ->where('email','=',$request->email)
                                  ->first();

    if($result){
        $tmp_pass = str_random(10);
        $user = User::create([
            'username'      => $result->name,
            'email'         => $request->email,
            'password'      => $tmp_pass,
            'tmp_pass'      => '',
            'active'        => 0,
            'client_code'   => $request->code
        ]);

        if($user){
            Mail::send('emails.verify',array('username' => $result->name, 'tmp_pass' => $tmp_pass), function($message) use ($user){
                $message->to($user->email, $user->username)
                    ->subject('Verify your Account');
            });
            return Redirect::to('/')
                ->with('message', 'Thanks for signing up! Please check your email.');
        }
        else{
            return Redirect::to('/')
                            ->with('message', 'Something went wrong');
        }
    }
    else{
        Session::flash('message', "Invalid code or email.");
        return redirect('/');
    }
}

Mail function used to work in Laravel 4 but I am getting errors in Laravel 5. Any help would be appreciated.

How to&Answers:

Mail is an alias inside the global namespace. When you want to reference it from inside a namespace (like App\Http\Controllers in your case) you have to either:

Prepend a backslash:

\Mail::send(...)

Or add a use statement before your class declaration:

namespace App\Http\Controllers;

use Mail;  // <<<<

class MyController extends Controller {

The same goes for the other facades you use. Like Session and Redirect.

Answer:

Another way is to use Mail facade

use Illuminate\Support\Facades\Mail;

In your controller

Answer:

In Laravel 5.8 I solved it by also adding this in the controller :

THIS:

use App\Mail\<<THE_NAME_OF_YOUR_MAIL_CLASS>>;
use Illuminate\Support\Facades\Mail;

INSTEAD OF:

use Mail;

Answer:

setup your app/config/mail.php

return array(

    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 465,
    'from' => array('address' => '[email protected]', 'name' => 'Welcome'),
    'encryption' => 'ssl',
    'username' => '[email protected]',
    'password' => 'passowrd',
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,

);

than setup in controller:

use Mail;

 \Mail::send('tickets.emails.tickets',array('ticketsCurrentNewId'=>
$ticketsCurrentNewId->id,'ticketsCurrentSubjectId'=>$ticketsCurrentNewId->subject,'ticketsCurrentLocationsObj'=>$ticketsCurrentLocationsObjname), function($message)
{
//$message->from('[email protected]');
$message->to('[email protected]', 'Amaresh')->subject(`Welcome!`);
});

after this setup mail are send emails if any permission error have showing then click this url and checked this radio button

https://www.google.com/settings/security/lesssecureapps

after configuration it is working fine in #laravel,#symfony and any php framework

thank you