无法与主机 smtp.gmail.com 建立连接 [权限被拒绝 #13]

Connection could not be established with host smtp.gmail.com [Permission denied #13]

我正在使用 Laravel-5.1 进行开发。我的 .env 配置文件在 windows 8、4GB RAM 的开发环境中正常工作。当我部署到 Centos-6 的远程服务器时,我开始得到这个错误:

Swift_TransportException in StreamBuffer.php line 265:
Connection could not be established with host smtp.gmail.com [Permission denied #13]
in StreamBuffer.php line 265
at Swift_Transport_StreamBuffer->_establishSocketConnection() in StreamBuffer.php line 62
at Swift_Transport_StreamBuffer->initialize(array('protocol' => 'tcp', 'host' => 'smtp.gmail.com', 'port' => '587', 'timeout' => '30', 'blocking' => '1', 'tls' => true, 'type' => '1')) in AbstractSmtpTransport.php line 113
at Swift_Transport_AbstractSmtpTransport->start() in Mailer.php line 79
at Swift_Mailer->send(object(Swift_Message), array()) in Mailer.php line 395
at Mailer->sendSwiftMessage(object(Swift_Message)) in Mailer.php line 181
at Mailer->send('email.verify', array('users' => object(User), 'confirmation_code' => 'kdsEeEcS0wGI6YYyzPJqwa0ZM0i10t'), object(Closure)) in Facade.php line 222
at Facade::__callStatic('send', array('email.verify', array('users' => object(User), 'confirmation_code' => 'kdsEeEcS0wGI6YYyzPJqwa0ZM0i10t'), object(Closure))) in UserController.php line 70
at Mail::send('email.verify', array('users' => object(User), 'confirmation_code' => 'kdsEeEcS0wGI6YYyzPJqwa0ZM0i10t'), object(Closure)) in UserController.php line 70
at UserController->store(object(UserRequest))
at call_user_func_array(array(object(UserController), 'store'), array(object(UserRequest))) in Controller.php line 256
at Controller->callAction('store', array(object(UserRequest))) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(UserController), object(Route), 'store') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(UserController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\UserController', 'store') in Route.php line 203
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 708
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54

这是 .env 配置文件:

APP_ENV=local
APP_DEBUG=true
APP_KEY=app_key_many_digits

DB_HOST=remotehost
DB_DATABASE=mydatabase
DB_USERNAME=mysqlusername
DB_PASSWORD=mysqlpassword

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

我不知道在 Centos-6 上是否默认配置了 smtp,或者我需要配置它,如果是,请指导我如何配置。

希望这对你有帮助,我为此花了很多时间。

我的解决方案原来是 SELinux 导致的问题。 这适用于 RedHat/CentOS 6.

允许 HTTPD(即 PHP)建立网络套接字连接

setsebool -P httpd_can_network_connect on

允许 HTTPD(即 PHP)发送邮件。

setsebool -P httpd_can_sendmail on

参考文献:

RedHat page about Security Enhanced Linux

Sending email with Swift Mailer, GMail and PHP, Permission Denied Error

Instructions on how to enable sendmail (and mail) for local system.