2013-01-05 168 views
18

我不爲我到底爲什麼會失敗的生活..這是我得到確切的錯誤:Swift_TransportException連接無法與主機smtp.gmail.com建立

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection 
could not be established with host smtp.gmail.com [Connection refused #111]' in 
/home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php:259 
Stack trace: #0 
/home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php(64): 
Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /home/content/38/6896038/html/test/lib/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 /home/content/38/6896038/html/test/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/content/38/6896038/html/test/contact.php(55): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php on line 259 

而且即時通訊使用的代碼是什麼,我從教程得到和閱讀這方面的例子,那就是:

require_once 'lib/swift_required.php'; 

$transport = Swift_MailTransport::newInstance(); 

// Create the Transport the call setUsername() and setPassword() 
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl') 
    ->setUsername('[email protected]') 
    ->setPassword('xxx') 
    ; 

// Create the Mailer using your created Transport 
$mailer = Swift_Mailer::newInstance($transport); 

$nombre = $_POST['name']; 
$apellido = $_POST['apellido']; 
$email = $_POST['email']; 
$telefono = $_POST['telefono']; 
$title = $_POST['jobtitle']; 

// Create the message 
$message = Swift_Message::newInstance() 

    // Give the message a subject 
    ->setSubject('Nuevo applicante para: ' . $title) 

    // Set the From address with an associative array 
    ->setFrom(array('[email protected]' => 'no-reply')) 

    // Set the To addresses with an associative array 
    ->setTo('[email protected]') 

    // Give it a body 
    ->setBody('<html> 
       <head></head> 
       <body> 
        <table> 
         <tr> 
          <td>Nombre:</td><td>' . $nombre . ' ' . $apellido .'</td> 
         </tr> 
         <tr> 
          <td>Email:</td><td>' . $email . '</td> 
         </tr> 
         <tr> 
          <td>Telefono:</td><td>'. $telefono .'</td> 
         </tr> 
        </table> 
       </body> 
      </html>', 'text/html') 

    // Optionally add any attachments 
    ->attach(Swift_Attachment::fromPath($_FILES["file"]["tmp_name"])->setFilename($_FILES['file']['name'])); 

// Send the message 
$result = $mailer->send($message); 

任何幫助是極大的讚賞,我已經做了我的閱讀和搜索,並不能找到一個解決方案這個:(

編輯:顯然,這是與Gmail的問題,我換到不同的SMTP和它的工作..:S

+0

看起來一切(代碼明智)設置是否正確,您是否嘗試通過您在此處使用的登錄信息登錄Google Apps?如果您剛剛創建了該帳戶,那麼gmail會要求您在帳戶可以使用之前同意一些條款。在該頁面上點擊「同意」之前,對SMTP的訪問將不可用。 – SamT

+0

是的,我有該電子郵件目前正在工作。 – Andres

+0

您的服務器是否允許通過465端口進行SSL連接?我知道一些共享主機,禁用此功能。如果是這種情況,通常您只需聯繫支持人員,他們將爲您開放該端口。 – Julien

回答

15

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection refused #111]

連接被拒絕是一個非常明確和清晰的錯誤消息。這意味着套接字連接無法建立,因爲遠程端主動拒絕連接。

Google阻止連接的可能性不大。

您的虛擬主機提供商很可能具有阻止端口465上的傳出連接的防火牆設置,或者它們將SMTP阻止到Gmail。 465是安全SMTP的「錯誤」端口,雖然它經常被使用,並且Gmail確實在那裏收聽。試試端口587。如果連接仍然被拒絕,請致電您的主持人並詢問他們發生了什麼事。

+0

我們如何爲此製作自定義錯誤? – Aaron

+0

@Charles,似乎你可以幫助我。看看這個:https://stackoverflow.com/questions/48654849/how-can-i-solve-connection-could-not-be-established-with-host-smtp-gmail-com –

-2

我剛剛遇到了這個問題,並通過在config文件夾下編輯mail.php來解決它。通常在使用共享主機時,他們託管公司允許您創建電子郵件,如

[email protected]

。因此,請轉到共享主機cpanel下的電子郵件設置並添加新電子郵件。拿這個電子郵件和密碼,並將其設置在

mail.php.

它應該工作!從config.yml

+1

如果OP使用Gmail的SMTP服務器,世界上的共享託管公司的配置如何相關?更不用說,一個不存在的電子郵件不太可能產生「連接被拒絕」的錯誤。 – kstev

6

刪除 spool: { type: memory }

然後,你將添加嘗試捕捉這樣

try{ 
     $mailer = $this->getMailer(); 
     $response = $this->getMailer()->send($message); 
    }catch(\Swift_TransportException $e){ 
     $response = $e->getMessage() ; 
    } 
+0

似乎你可以幫助我。看看這個:https://stackoverflow.com/questions/48654849/how-can-i-solve-connection-could-not-be-established-with-host-smtp-gmail-com –

0

我有一會兒了同樣的問題,免去:smtp.gmail.com173.194.65.108實際爲我工作!

0

以我爲例,我是用Laravel 5,我已經忘了改在位於目錄根目錄下的文件.ENV郵件全局(這些變量將覆蓋郵件配置)

MAIL_DRIVER=smtp 
    MAIL_HOST=smtp.gmail.com 
    MAIL_PORT=465 
    [email protected] 
    MAIL_PASSWORD=yourpassword 

默認情況下,郵件主機是:

MAIL_HOST=mailtraper.io 

我得到了同樣的錯誤,但對我工作。

2

tcp:465被阻止。嘗試添加新的防火牆規則並添加規則端口465.或檢查587並將加密更改爲tls。

3

我有同樣的問題,當我使用Godaddy的虛擬主機,並通過編輯我的.ENV文件解決了這個,

MAIL_DRIVER=smtp 
MAIL_HOST=XXXX.XXXX.in 
MAIL_PORT=587 
MAIL_USERNAME=dexxxxx 
MAIL_PASSWORD=XXXXXXXX 
MAIL_ENCRYPTION=tls 

哪裏MAIL_HOST是從GoDaddy的域名,MAIL_USERNAME是你的用戶名來自Godaddy和MAIL_PASSWORD是來自Godaddy的密碼。

我希望這可以幫助你。

+0

我在哪裏可以找到。 env文件?我也在使用godaddy!謝謝! – Gacci

+0

默認情況下,它不會在cpanel上可見。你可以使用ftp連接來查看這個文件。它將成爲你項目的根源 –

-2

我有同樣的問題,我設法通過禁用我的AVAST防病毒來修復它。以及我在瀏覽器中的AVAST擴展。防病毒有時會阻止您的應用程序。 :)我希望它對任何人都有幫助。

+0

用步驟或例子來探索它,以便更好地理解 – Anil

1

我的解決辦法是改變:

'driver' => 'smtp', 

'driver' => 'mail', 

在應用程序/配置/郵件

希望幫助

相關問題