我不断收到此错误' Class' App \ Http \ Controllers \ Mail'找不到'我的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('/');
}
}
邮件功能曾在Laravel 4中工作,但我在Laravel 5中遇到错误。任何帮助都将不胜感激。
答案 0 :(得分:21)
Mail
是全局命名空间中的别名。如果要从命名空间内部引用它(例如在您的情况下为App\Http\Controllers
),则必须:
预先加上反斜杠:
\Mail::send(...)
或者在您的课程声明之前添加use
声明:
namespace App\Http\Controllers;
use Mail; // <<<<
class MyController extends Controller {
您使用的其他外墙也是如此。与Session
和Redirect
一样。
答案 1 :(得分:4)
另一种方法是使用Mail facade
use Illuminate\Support\Facades\Mail;
在您的控制器中
答案 2 :(得分:2)
在Laravel 5.8中,我还通过将其添加到控制器中来解决了这个问题:
这个:
use App\Mail\<<THE_NAME_OF_YOUR_MAIL_CLASS>>;
use Illuminate\Support\Facades\Mail;
代替:
use Mail;
答案 3 :(得分:1)
设置您的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,
);
比在控制器中设置:
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!`);
});
此安装邮件发送电子邮件后,如果显示任何权限错误,请单击此URL并选中此单选按钮
https://www.google.com/settings/security/lesssecureapps
配置完成后,#laravel,#symfony和任何php框架
工作正常谢谢