【问题标题】:connection could not be established with host smtp.googlemail.com [A connection attempt failed无法与主机 smtp.googlemail.com 建立连接 [连接尝试失败
【发布时间】:2021-06-10 10:25:57
【问题描述】:

我正在使用 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}"  

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

  ];

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

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

【问题讨论】:

  • 如果您的环境值包含特殊字符,那么您必须使用引号
  • 感谢回复,app密码全是普通字符
  • 我厌倦了在密码上使用引号但仍然是同样的错误
  • @kareem.i 已经发布了我的答案。您可以根据我提供的内容更改环境配置。如果有任何问题请告诉我

标签: laravel email gmail verify


【解决方案1】:

将您的 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_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=education123online@gmail.com MAIL_PASSWORD='app password' MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=education123online @gmail.com MAIL_FROM_NAME="${APP_NAME}"
  • 'default' => env('MAIL_MAILER', 'smtp'), 'mailers' => [ 'smtp' => [ 'transport' => 'smtp', 'host' => env('MAIL_HOST', 'smtp.gmail.com'), 'port' => env('MAIL_PORT', 465), '加密' => env('MAIL_ENCRYPTION', 'ssl'), '用户名' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, 'auth_mode' => null, ],
  • 实际上问题出在您的配置文件中,您没有获得 env 常量,而是通过发送电子邮件获得 .env 常量
猜你喜欢
  • 2019-03-11
  • 2019-01-04
  • 2011-04-30
  • 1970-01-01
  • 2018-09-03
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多