php 无法与主机 mailtrap.io 建立连接 [php_network_getaddresses: getaddrinfo failed: No such host is known. #0]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41690219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:26:53  来源:igfitidea点击:

Connection could not be established with host mailtrap.io [php_network_getaddresses: getaddrinfo failed: No such host is known. #0]

phplaravel-5.2laravel-mail

提问by Harsimer

I am getting below error:

我收到以下错误:

Connection could not be established with host smtp.gmail.com [php_network_getaddresses: getaddrinfo failed: No such host is known.

0]

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

0]

My .env file looks like:

我的 .env 文件看起来像:

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

My mail.php file inside config looks like:

我在配置中的 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',

];

Surprisingly I am using this for last 6 months and it didn't error earlier but now when I uploaded my built again on staging I got this error.

令人惊讶的是,我在过去 6 个月里一直在使用它,而且它没有更早出错,但是现在当我在登台时再次上传我的构建时,我收到了这个错误。

Any suggestions would be highly appreciated.

任何建议将不胜感激。

回答by Yogesh Nogia

This problem occurs when some changes have been made to files, specifically in .env file

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

Clear cache using artisan command

使用 artisan 命令清除缓存

php artisan cache:clear

Clear config

清除配置

php artisan config:clear

Restart your server

重启你的服务器

sudo service apache2 restart

More: Try clearing browsers cache & cookies. Hope that works

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

回答by Ferdie De Oliveira

I just had this same problem.

我刚刚遇到了同样的问题。

It looks like the server is not able to ping the destination address.

服务器似乎无法 ping 目标地址。

I did a quick restart of my dev server and everything is back to normal.

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

Hope that helps.

希望有帮助。

回答by Pravin

Use below code in .envfile:

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

MAIL_ENCRYPTION= TLS

Try to clear cache, config cache, and restart apache2.

尝试清除cacheconfig cacherestart apache2

It works.

有用。

回答by Stefan C

After modify your settings you need to run: php artisan config:cache

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

Without those laravel will use old ones.

如果没有这些,laravel 将使用旧的。

回答by Ramya

.env

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=yourmailIDpassword
MAIL_ENCRYPTION=tls

config/mail.php

配置/mail.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', '[email protected]'),
    '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'),

];

app/Http/Controllers/yourcontroller.php

app/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('[email protected]','DoNotReply');
  });
}

then create folder under app.. the folder name is Mail under this folder create sendMail.php file

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

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

then add resources/views/mail.blade.php

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

mail.blade.php

邮件.blade.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>

回答by Derrick

Edit .envfile :

编辑.env文件:

MAIL_HOST=smtp.gmail.com

Edit Config/mail.phpfile :

编辑Config/mail.php文件:

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

.envand mail.phpshould both correspond to each other .

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

回答by Chukky Nze

The problem is actually a simple typo. In your .env file, change this:

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

MAIL_DRIVER=smtp MAIL_HOST=mailtrap.io

MAIL_DRIVER=smtp MAIL_HOST=mailtrap.io

to this:

对此:

MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io

MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io

I prepended the smtp.to the host

smtp.在主持人前面加了

回答by Tola LENG

Edit and following this file: config / mail.php

编辑并关注此文件: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'),

Don't touch for these two lines

这两行不要碰

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

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

Edit and following this file: /.env

编辑并关注此文件:/.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"

Use below artisan command.

使用以下工匠命令。

Clear cache using artisan command

使用 artisan 命令清除缓存

php artisan config:clear

php工匠配置:清除

Clear config

清除配置

php artisan cache:clear

php工匠缓存:清除

Restart your server

重启你的服务器

sudo service httpd restart

须藤服务 httpd 重启

It will be works! Enjoy your coding

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