问题描述
我正在使用 laravel 8,即使我通过 StackOverflow 和 youtube 搜索它并尝试了一切,我仍然收到此错误
1- 我在 Gmail 上启用了安全性较低的应用
2- 我在 gmail 上创建了 2 个身份验证并生成了应用密码并使用了它
环境。文件
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME= education123online@gmail.com
MAIL_PASSWORD= ' app password'
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=education123online@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
<?PHP
return [
/*
|--------------------------------------------------------------------
------
| Default Mailer
|-----------------------------------------------------------------
---------
|
| This option controls the default mailer that is used to send any
email
| messages sent by your application. Alternative mailers may be
setup
| and used as needed; however,this mailer will be used by default.
|
*/
'default' => env('MAIL_MAILER','smtp'),/*
|-------------------------------------------------------------------
-------
| Mailer Configurations
|--------------------------------------------------------------------
------
|
| Here you may configure all of the mailers used by your application
plus
| their respective settings. Several examples have been configured for
| you and you are free to add your own as your application requires.
|
| Laravel supports a variety of mail "transport" drivers to be used
while
| sending an e-mail. You will specify which one you are using for your
| mailers below. You are free to add additional mailers as required.
|
| Supported: "smtp","sendmail","mailgun","ses",| "postmark","log","array"
|
*/
'mailers' => [
'smtp' => [
'transport' => 'smtp','host' => env('MAIL_HOST','smtp.mailgun.org'),'port' => env('MAIL_PORT',587),'encryption' => env('MAIL_ENCRYPTION','tls'),'username' => env('MAIL_USERNAME'),'password' => env('MAIL_PASSWORD'),'timeout' => null,'auth_mode' => null,],'ses' => [
'transport' => 'ses','mailgun' => [
'transport' => 'mailgun','postmark' => [
'transport' => 'postmark','sendmail' => [
'transport' => 'sendmail','path' => '/usr/sbin/sendmail -bs','log' => [
'transport' => 'log','channel' => env('MAIL_LOG_CHANNEL'),'array' => [
'transport' => 'array',/*
|-----------------------------------------------------------------------
---
| Global "From" Address
|-----------------------------------------------------------------------
---
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here,you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS','hello@example.com'),'name' => env('MAIL_FROM_NAME','Example'),/*
|-----------------------------------------------------------------------
---
| Markdown Mail Settings
|-----------------------------------------------------------------------
---
|
| If you are using Markdown based email rendering,you may configure
your
| theme and component paths here,allowing you to customize the design
| of the emails. Or,you may simply stick with the Laravel defaults!
|
*/
'markdown' => [
'theme' => 'default','paths' => [
resource_path('views/vendor/mail'),];
MailController.PHP
<?PHP
namespace App\Http\Controllers;
use App\Mail\TestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function sendEmail()
{
$details = [
'title' => 'Mail from Surfside media','body' => 'This is for testing mail using gmail'
];
Mail::to("education123online@gmail.com")->send(new TestMail($details));
return "Email Sent";
}
}
TestMail.PHP
<?PHP
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class TestMail extends Mailable
{
use Queueable,SerializesModels;
public $details;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Test Mail From Surfside Media')->view('emails.TestMail');
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<Meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test Mail</title>
</head>
<body>
<h1> {{$details['title']}}</h1>
<p> {{$details['body']}}</p>
<p> Thank you</p>
</body>
</html>
Route::middleware(['auth::sanctum','verified'])->get('/dashboard',function () {
return view('dashboard');
})->name('dashboard');
Route::get('/email/verify',function () {
return view('auth.verify-email');
})->middleware('auth')->name('verification.notice');
Route::get('/email/verify/{id}/{hash}',function (EmailVerificationRequest $request) {
$request->fulfill();
return redirect('/home');
})->middleware(['auth','signed'])->name('verification.verify');
Route::post('/email/verification-notification',function (Request $request) {
$request->user()->sendEmailVerificationNotification();
return back()->with('message','Verification link sent!');
})->middleware(['auth','throttle:6,1'])->name('verification.send');
Route::get('/send-email',[MailController::class,'sendEmail']);
解决方法
将您的 Mail.php 文件更改为 config 文件夹中的此文件,您将错误地设置为 .env 常量
'default' => env('MAIL_MAILER','smtp'),'mailers' => [
'smtp' => [
'transport' => 'smtp','host' => env('MAIL_HOST','smtp.gmail.com'),'port' => env('MAIL_PORT',465),'encryption' => env('MAIL_ENCRYPTION','ssl'),'username' => env('MAIL_USERNAME'),'password' => env('MAIL_PASSWORD'),'timeout' => null,'auth_mode' => null,],