Laravel 无法与主机smtp.googlemail.com建立连接[连接尝试失败]
我使用的是Laravel8,即使我通过StackOverflow和youtube搜索并尝试了所有方法,仍然会出现这个错误 1-我在gmail上启用了不太安全的应用程序 2-我在gmail上创建了2个身份验证,生成了应用程序密码并使用了它 但我仍然收到了这个错误,没有给gmail发送电子邮件 环境文件Laravel 无法与主机smtp.googlemail.com建立连接[连接尝试失败],laravel,email,gmail,verify,Laravel,Email,Gmail,Verify,我使用的是Laravel8,即使我通过StackOverflow和youtube搜索并尝试了所有方法,仍然会出现这个错误 1-我在gmail上启用了不太安全的应用程序 2-我在gmail上创建了2个身份验证,生成了应用程序密码并使用了它 但我仍然收到了这个错误,没有给gmail发送电子邮件 环境文件 MAIL_DRIVER=smtp MAIL_HOST=smtp.googlemail.com MAIL_PORT=587 MAIL_USERNAME= education123online@gmai
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}"
mail.php文件
<?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'),
],
],
];
将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,
],
感谢您的回复!但我已经打开了不太安全的应用程序访问,tls或ssl仍然显示错误预期响应代码220,但得到了带有消息“”的代码“”,它在我使用tls时显示了此错误,这是唯一的区别,而不是上面的区别。请从.env变量中删除空格,例如:MAIL\u DRIVER=smtp MAIL\u HOST=smtp.gmail.com MAIL\u PORT=465 MAIL\u USERNAME=education123online@gmail.comMAIL\u PASSWORD='app PASSWORD'MAIL\u ENCRYPTION=tls MAIL\u FROM\u地址=education123online@gmail.com寄自_NAME=“${APP_NAME}”'default'=>env('MAIL_MAILER','smtp'),'mailers'=>['smtp'=>'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,],实际上问题是在你的配置文件中,你没有得到你正在得到的环境常数。如果你的环境常数包含特殊字符,那么你必须使用quotesthanks进行回复,应用程序密码只是充满了普通字符。虽然我厌倦了在密码上使用引号,但仍然一样error@kareem.i有posted我的答案。你可以根据我提供的更改环境配置。如果有任何问题,请告诉我
<?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']);
'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,
],