我正在尝试在用户注册时向他们发送电子邮件验证链接,但我收到一条消息Authentication required,但没有发送邮件。我尝试将 mailtrap 用于演示和 sendgrid,我将在生产中使用它们,但消息是相同的。这就是我要做的
运行后,composer require guzzlehttp/guzzle我像这样更新了我的 env 文件
# MAIL_DRIVER=smtp
# MAIL_HOST=smtp.mailtrap.io
# MAIL_PORT=2525
# MAIL_USERNAME=mailtrap_username
# MAIL_PASSWORD=mailtrap_password
# MAIL_ENCRYPTION=tls
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgrid_username
MAIL_PASSWORD=sendgrid_password
MAIL_ENCRYPTION=tls
在控制器中,我想在这样成功创建用户后发送邮件
...
use App\Mail\VerifyEmail;
...
use Illuminate\Support\Facades\Mail;
public function register(Request $request)
{
// create and store new user record
$user = User::create([
'username' => $request->username,
'password' => bcrypt($request->password)
]);
// send user email verification link
Mail::to($user->username)->send(new VerifyEmail());
}
验证邮件.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class VerifyEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$from = 'support@fromus.com';
$name = 'custom name';
$subject = 'Welcome! Confirm Your Email';
return $this->from($from, $name)
->subject($subject)
->view('auth.verify');
}
}
按照电子邮件验证文档https://laravel.com/docs/5.8/verification#verification-routing我添加了Auth::routes(['verify' => true])这样的api.php文件
<?php
// Register routes for email verification
Auth::routes(['verify' => true]);
Route::prefix('v1')->group(function () {
// protected routes
Route::middleware('auth:api')->group(function () {
Route::get('products', 'ProductController@index'); // get products
});
});
为什么我会收到Authentication required错误消息,我该如何解决?
宝慕林4294392