15

我收到以下错误:

无法与主机 smtp.gmail.com 建立连接 [php_network_getaddresses: getaddrinfo failed: No such host is known。

0]

我的 .env 文件如下所示:

MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xxxxxxxxxxxxxx
MAIL_PASSWORD=xxxxxxxx
#MAIL_ENCRYPTION=null

我在配置中的 mail.php 文件如下所示:

/*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill",
    |            "ses", "sparkpost", "log"
    |
    */

    'driver' => env('MAIL_DRIVER', 'smtp'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Address
    |--------------------------------------------------------------------------
    |
    | Here you may provide the host address of the SMTP server used by your
    | applications. A default option is provided that is compatible with
    | the Mailgun mail service which will provide reliable deliveries.
    |
    */

    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */

    'port' => env('MAIL_PORT', 587),

    /*
    |--------------------------------------------------------------------------
    | 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' => null, 'name' => null],

    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */

    'username' => env('MAIL_USERNAME'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Password
    |--------------------------------------------------------------------------
    |
    | Here you may set the password required by your SMTP server to send out
    | messages from your application. This will be given to the server on
    | connection so that the application will be able to send messages.
    |
    */

    'password' => env('MAIL_PASSWORD'),

    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -bs',

];

令人惊讶的是,我在过去的 6 个月里一直在使用它,它之前没有出错,但是现在当我在 staging 上再次上传我的构建时,我得到了这个错误。

任何建议将不胜感激。

4

9 回答 9

19

当对文件进行了一些更改时会出现此问题,特别是在 .env 文件中

使用 artisan 命令清除缓存

php artisan cache:clear

清除配置

php artisan config:clear

重启你的服务器

sudo service apache2 restart

更多:尝试清除浏览器缓存和 cookie。希望有效

于 2018-03-30T10:05:27.323 回答
5

我刚遇到同样的问题。

看起来服务器无法 ping 目标地址。

我快速重启了我的开发服务器,一切恢复正常。

希望有帮助。

于 2017-03-22T16:51:17.623 回答
3

在文件中使用以下代码.env

MAIL_ENCRYPTION= TLS

尝试清除cacheconfig cacherestart apache2

有用。

于 2018-05-15T07:52:59.960 回答
3

修改设置后,您需要运行: php artisan config:cache

没有这些 laravel 将使用旧的。

于 2017-04-05T10:46:50.233 回答
2

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourmail@gmail.com
MAIL_PASSWORD=yourmailIDpassword
MAIL_ENCRYPTION=tls

配置/邮件.php

?php

return [

/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
|            "sparkpost", "log", "array"
|
*/

'driver' => env('MAIL_DRIVER', 'smtp'),

/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/

'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/

'port' => env('MAIL_PORT', 587),

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

/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/

'encryption' => env('MAIL_ENCRYPTION', 'tls'),

/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/

'username' => env('MAIL_USERNAME'),

'password' => env('MAIL_PASSWORD'),

/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/

'sendmail' => '/usr/sbin/sendmail -bs',

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

/*
|--------------------------------------------------------------------------
| Log Channel
|--------------------------------------------------------------------------
|
| If you are using the "log" driver, you may specify the logging channel
| if you prefer to keep mail messages separate from other log entries
| for simpler reading. Otherwise, the default channel will be used.
|
*/

'log_channel' => env('MAIL_LOG_CHANNEL'),

];

应用程序/Http/Controllers/yourcontroller.php

use App\Mail\sendMail;
use Mail;

public function fnmail($name,$password,$userid,$email)
{ 
  $data = array('name'=>$name, 'password' => $password, 'userid' => $userid);
  Mail::send('mail', $data, function($message) use ($name, $email) {
  $message->to($email, $name)
        ->subject('Subject');  //to redirect mail.blade.php page
  $message->from('YourMail@gmail.com','DoNotReply');
  });
}

然后在app下创建文件夹..文件夹名称是Mail在这个文件夹下创建sendMail.php文件

发送邮件.php

<?php

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class sendMail extends Mailable
{
use Queueable, SerializesModels;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('register'); //to redirect register page
}
}

然后添加资源/视图/mail.blade.php

邮件刀片.php

<!DOCTYPE html>
<html>
<head>
  <title>Mail Sending</title>
</head>
<body>
  <table align='center' style='height: 200px;width: 200px'>
   <tr><td colspan='2' bgcolor='skyblue' style='text-align: center;font-weight: 
  bold;size: 20px;'><label> Mail Check </label></td></tr>
   <tr bgcolor='#fff7e6'>
   <tr><td style='color: magenta;font-weight: bold;'><label><b>Your UserName:</b> 
 </label></td><td><b>{{$name}}</b></td></tr><br>
   <tr><td style='color: magenta;font-weight: bold;'><label><b>Your LoginID:</b> 
 </label></td><td><b>{{$userid}}</b></td></tr><br>
 <tr><td style='color: magenta;font-weight: bold;'><label><b>Your Password:</b> 
 </label></td><td><b>{{$password}}</b></td><br></tr>
</tr>
<tr><td colspan='2' align='center' bgcolor='orange'><a 
href='http://localhost/larashop/laravelProject/public/login'>
<button>Login</button></a></td></tr></table>
</body>
</html>
于 2019-07-22T06:53:27.337 回答
2

这个问题实际上是一个简单的错字。在您的 .env 文件中,更改以下内容:

MAIL_DRIVER=smtp MAIL_HOST=mailtrap.io

对此:

MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io

smtp.我在主持人前面

于 2018-09-20T07:54:29.930 回答
0

编辑并关注这个文件:config/mail.php

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'your mail host'),
'port' => env('MAIL_PORT', '465'),
'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'Your email address'),
        'name' => env('MAIL_FROM_NAME', 'Your email Name'),
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),

不要碰这两行

'username' => env('MAIL_USERNAME'),

'password' => env('MAIL_PASSWORD'),

编辑并关注此文件:/.env

MAIL_DRIVER=smtp
MAIL_HOST=your mail host
MAIL_PORT=465
MAIL_USERNAME=Your smtp email address
MAIL_PASSWORD=Your smtp password
MAIL_ENCRYPTION=ssl

MAIL_FROM_ADDRESS=Your email address
MAIL_FROM_NAME="Your email Name"

使用以下工匠命令。

使用 artisan 命令清除缓存

php工匠配置:清除

清除配置

php工匠缓存:清除

重启你的服务器

sudo 服务 httpd 重启

这将是工作!享受你的编码

于 2019-02-13T08:49:29.610 回答
0

编辑.env文件:

MAIL_HOST=smtp.gmail.com

编辑Config/mail.php文件:

'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

.env并且mail.php都应该相互对应。

于 2019-09-19T07:19:43.513 回答
0

这是因为 Enctype 没有给出正确的。检查config/mail.php.env

'encryption' => env('MAIL_ENCRYPTION', 'tsl'),

加密 tsl、ssl 或任何其他 enctype。我也遇到过同样的问题。

于 2021-08-25T05:24:43.407 回答