Mail::send() not working in Laravel 5

2020-06-17 04:18发布

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.

4条回答
够拽才男人
2楼-- · 2020-06-17 04:59

Another way is to use Mail facade

use Illuminate\Support\Facades\Mail;

In your controller

查看更多
看我几分像从前
3楼-- · 2020-06-17 05:02

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;
查看更多
smile是对你的礼貌
4楼-- · 2020-06-17 05:12

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.

查看更多
Fickle 薄情
5楼-- · 2020-06-17 05:15

setup your app/config/mail.php

return array(

    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 465,
    'from' => array('address' => 'your@gmail.com', 'name' => 'Welcome'),
    'encryption' => 'ssl',
    'username' => 'your@gmail.com',
    '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('your@gmail.com');
$message->to('your@gmail.com', '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

查看更多
登录 后发表回答