首页 > 解决方案 > 无法与主机 smtp.googlemail.com 建立连接 [连接尝试失败

问题描述

我正在使用 laravel 8,即使我通过 StackOverflow 和 youtube 搜索它并尝试了所有方法,我仍然会收到此错误

1- 我在 gmail 上启用了不太安全的应用程序

2-我在 gmail 上创建了 2 个身份验证并生成了应用程序密码并使用了它

但我仍然收到此错误,并且没有电子邮件发送到 gmail

环境。文件

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 文件

<?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'),
    ],
],

  ];

邮件控制器.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";
}
}

测试邮件.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');
}
 }

TestMail.blade.php 文件

       <!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>

web.php(路由文件)

      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']);

标签: laravelemailgmailverify

解决方案


在配置文件夹中更改您的 Mail.php 文件,您错误地获取了 .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,
    ],

推荐阅读