2014-08-29 160 views
12

當我嘗試使用Gmail的SMTP通過Laravel發送電子郵件,我會遇到以下錯誤:通過Laravel爲使用Gmail SMTP:連接無法與主機smtp.gmail.com建立[連接超時#110]

Swift_TransportException 

Connection could not be established with host smtp.gmail.com [Connection timed out #110] 

這是錯誤的軌跡:

... 
} 
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options)); 
if (false === $this->_stream) { 
throw new Swift_TransportException(
'Connection could not be established with host ' . $this->_params['host'] . 
' [' . $errstr . ' #' . $errno . ']'... 

,這裏是我的郵件配置:

'driver' => 'smtp', 

'host' => 'smtp.gmail.com', 

'port' => 587, 

'from' => array('address' => '[email protected]', 'name' => 'some'), 

'encryption' => 'tls', 

'username' => '[email protected]', 

'password' => 'mypassword', 

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

'pretend' => false 

我使用共享主機,並且localhost上的端口587已打開。

回答

1

嘗試

'encryption' => 'ssl', 

'port' => 465, 
0

如果它不是Google Apps帳戶,則建議您可以使用安全性較低的應用進行訪問。如果你不這樣做,它不會工作。

如果它是一個Google Apps帳戶(即它是一個商業帳戶),那麼有一個管理面板來管理訪問。您需要確保它只是通過身份驗證指定訪問權限,而不是通過IP指定訪問權限,因爲如果通過IP,您的IP大概不在列表中。

最後要嘗試的是使用smtp.gmail.com的IPv4地址代替代碼中的域名。我發現我的連接不會使用域(因爲解析爲IPv6地址),而是會在我使用原始IP時連接。

26

我有同樣的問題,我解決它以這樣的方式

'driver' => 'sendmail', 

你只需要改變該行。

+0

你用Gmail的smtp?那麼我認爲司機應該是smtp,不是? – Ahmad 2015-01-23 18:18:43

+1

這是通過,但**電子郵件**不發送。 – ihue 2015-11-10 13:20:46

+0

我在哪裏可以找到這些配置? – mr5 2016-02-09 07:51:25

3

問題是smtp.gmail.com正在解析IPv6地址,而且Google服務只能偵聽IPv4。你需要做的是設置源IP,以確保域名解析爲IPv4而不是IPv6。

的重要方法:

->setSourceIp('0.0.0.0') 

瞭如何在代碼中使用它:啓用

$this->_transport = Swift_SmtpTransport::newInstance 
     (
      'smtp.gmail.com', 
      465, 
      'ssl' 
     ) 
      ->setUsername('username') 
      ->setSourceIp('0.0.0.0') 
      ->setPassword('password'); 
+0

我可以在哪裏設置? 也許你可以幫助我。看看這個:https://stackoverflow.com/questions/48654849/how-can-i-solve-connection-could-not-be-established-with-host-smtp-gmail-com – 2018-02-07 09:40:18

0

該錯誤可能是由於兩步驟驗證。在這種情況下,您需要創建gmail應用程序密碼並將其用作密碼。

4

適用於我,除了加密和端口相同的設置。更改爲:

'encryption' => ssl, 
'port' => 465, 

由於這僅適用於本地主機加密行,因此也應該是特定於環境的。所以不是上面我沒有以下內容:

env('MAIL_ENCRYPTION','tls'), 

現在你可以在.ENV文件,這是特定環境,應該是在的.gitignore

+0

這一個適合我! – codecarver 2017-08-31 09:17:39

0

我使用Swiftmailer

反正同樣的問題設置此,一個你不應該使用的快速骯髒的黑客將編輯swiftmailer \ swiftmailer \ lib \ classes \ Swift \ Transport \ StreamBuffer.php。在_establishSocketConnection()線253取代:

$options = array(); 

像這樣的東西:

$options = array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)); 

這將改變stream_context_create的)的SSL選項((幾行低於$選項):

$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, 
$errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options)); 

這裏有一個骯髒的黑客You can find it here

此外我的ENV是設置爲

MAIL_DRIVER=smtp 
MAIL_HOST=mail.mydomain.com 
MAIL_PORT=587 
[email protected] 
MAIL_PASSWORD=mypassword 
0

此命令優先考慮的IPv4在你的終端使用

須藤UFW允許 「後綴提交」 這使用於SMTP的端口587

2

通過更改我的.env文件解決了我的問題,如下所示:

'driver' => 'sendmail', 
+0

這對我工作.. – 2017-09-18 04:33:20

0

您需要在Google帳戶中創建2個因子認證和自定義密碼。另外不要忘記爲每個正在使用的新主機添加自定義密碼。

相關問題