php Mail::send() 在 Laravel 5 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28883894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:07:39  来源:igfitidea点击:

Mail::send() not working in Laravel 5

phpemaillaravel

提问by user3127109

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

我在 UserController.php 中不断收到此错误 'Class 'App\Http\Controllers\Mail' not found' 错误

    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.

邮件功能曾经在 Laravel 4 中工作,但我在 Laravel 5 中遇到错误。任何帮助将不胜感激。

回答by lukasgeiter

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

Mail全局命名空间内的别名。当您想从命名空间内部引用它时(如App\Http\Controllers您的情况),您必须:

Prepend a backslash:

前置反斜杠:

\Mail::send(...)

Or add a usestatement before your class declaration:

或者use在你的类声明之前添加一条语句:

namespace App\Http\Controllers;

use Mail;  // <<<<

class MyController extends Controller {

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

您使用的其他外观也是如此。喜欢SessionRedirect

回答by user3127109

Another way is to use Mail facade

另一种方法是使用邮件门面

use Illuminate\Support\Facades\Mail;

In your controller

在您的控制器中

回答by kevind

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

在 Laravel 5.8 中,我通过在控制器中添加以下内容解决了这个问题:

THIS:

这个:

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

INSTEAD OF:

代替:

use Mail;

回答by Amaresh Kumar

setup your app/config/mail.php

设置你的 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

在此设置邮件后发送电子邮件,如果显示任何权限错误,则单击此 url 并选中此单选按钮

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

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

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

配置后,它在#laravel、#symfony 和任何 php 框架中都可以正常工作

thank you

谢谢你