2013-10-07 125 views
4

我看着下面的鏈接:的PHPMailer使用SMTP與Gmail不工作 - 連接超時

phpmailer send gmail smtp timeout

send email using Gmail SMTP server through PHP Mailer

http://uly.me/phpmailer-and-gmail-smtp/

...並試圖實施我自己是那些的組合...大多數時間它發送此消息...

Message could not be sent.

Mailer Error: SMTP connect() failed.

但是有一次在那裏發回當我「TLS」和「SSL」之間嘗試...

SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Message could not be sent.

Mailer Error: SMTP connect() failed.

我的代碼附...爲什麼我莫名其妙地錯過了什麼?我問網絡託管服務是否阻止,並給他們一個我的代碼模板 - 他們說服務器允許連接到Gmail的SMTP。

require_once("class.phpmailer.php"); 
    $mail = new PHPMailer(); 
    $mail -> IsSMTP(); 
    $mail -> SMTPDebug = 2; 
    $mail -> SMTPAuth = 'true'; 
    $mail -> SMTPSecure = 'tls'; 
    $mail -> SMTPKeepAlive = true; 
    $mail -> Host = 'smtp.gmail.com'; 
    $mail -> Port = 587; 
    $mail -> IsHTML(true); 

    $mail -> Username = "[email protected]"; 
    $mail -> Password = "mypassword"; 
    $mail -> SingleTo = true; 

    $to = xxx;       
    $from = xxx; 
    $fromname = xxx; 
    $subject = xxx; 
    $message = xxx 
    $headers = "From: $from\n"; 
    $headers .= "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 

    $mail -> From = $from; 
    $mail -> FromName = $fromname; 
    $mail -> AddAddress($to); 

    $mail -> Subject = $subject; 
    $mail -> Body = $message; 

    if(!$mail -> Send()){ 
     echo "Message could not be sent. <p>"; 
     echo "Mailer Error: " . $mail-> ErrorInfo; 
     exit; 
    } 
+0

順便說一下,它對我自己的本地主機非常好,如果我發送我從那裏使用郵件 - 當我上傳到主機,但它錯誤。 – redber2009

+0

我也研究過[鏈接](http://stackoverflow.com/questions/2867017/phpmailer-with-gmail-smtp-error)它沒有幫助,因爲我試驗了ssl和tls,它給出了相同的輸出 – redber2009

+0

我編輯了我的答案,看看你是否錯過了它 – Krimson

回答

1

使用SSL

$mail -> SMTPSecure = 'ssl'; 

端口應該是465

$mail -> Port = 465; 

更改您的主機:

$mail -> Host = 'ssl://smtp.gmail.com'; 

希望它可以工作

+0

我已經試過這個@vidhu - 它似乎沒有解決問題。我認爲它不是ssl和tls之間的問題...... – redber2009

+0

雖然感謝;-) – redber2009

+0

@ redber2009那麼我恐怕不能說它是你的機器,它正在表演。 :S – Krimson

1

檢查以確保您可以從您的網絡主機到達Gmail。我假設它是Linux。 SSH並在命令行中輸入

telnet smtp.gmail.com 587 

應該會得到

Connected to smtp.something 

它必須是本地主機和您的供應商

+0

這是intersting凱西 - 不知道如何做到這一點,雖然...對於本地連接到Gmail的代碼作品...至於從我的遠程虛擬主機的連接 - 他們告訴我他們的服務器允許連接到Gmail。我要澄清配置嗎? – redber2009

+0

你可以SSH入你的網絡服務器嗎?你需要知道的唯一的事情是如果端口587被阻塞。如果他們告訴你,這並不意味着它不是。你使用的是什麼虛擬主機,是共享主機? – Casey

+0

它是共享主機 - 一個你可以得到低於5美元一個月。 Webhostingpad – redber2009

8

我挖成這之間的配置差異。使用php原生的fsocketopen來測試連接並消除大部分潛在問題。用這個寫一個簡單的PHP頁面:

$host = "smtp.gmail.com"; 
    $port = "587"; 
    $checkconn = fsockopen($host, $port, $errno, $errstr, 5); 
    if(!$checkconn){ 
     echo "($errno) $errstr"; 
    } else { 
     echo 'ok'; 
    } 

你應該回來「好」。如果沒有,你知道你有一個與Phpmailer無關的連接問題。如果是這種情況,它可能是託管公司。如果不是,那麼這可能是簡單的關於您的本地主機和託管公司之間的區別,如不同版本的PHP。

我懷疑,雖然這個腳本不會使連接

+0

很酷的凱西!它說「OK」......這意味着它與託管公司有關。無論如何,我正在嘗試使用SSL現在...錯誤現在是SMTP - >錯誤:無法連接到服務器:連接超時(110)SMTP連接()失敗 – redber2009

+0

它表示託管公司isn阻止港口。必須是一些配置問題與phpMailer – Casey

+0

我有類似的問題阻止/未阻止。鏈接到這裏的可見性https://stackoverflow.com/questions/32827193/outbound-port-open-on-firewall-how-to-unblock-for-php –

0

這也許可能是Gmail的阻止您訪問。

轉到您的安全配置,看看它的阻止任何訪問..

或嘗試更改您的密碼,然後再試一次。

2

我有這個同樣的問題,解決了這個問題:

首先,打開SMTP錯誤日誌中的PHPMailer:

$mail->SMTPDebug = 2;  // enables SMTP debug information (for testing) 

然後重新嘗試PHPMailer的電子郵件發送。您將在標準錯誤輸出中看到整個SMTP對話。如果您使用的是Web服務器,請查看Web服務器日誌文件。

然後我可以看到從Gmail中的錯誤響應。 Gmail不接受登錄信息。

SMTP會話中的錯誤是指article。它給出了一些提示:

  1. Allow less secure apps to use your account
  2. 從網絡瀏覽器登錄到gmail。
  3. 確認gmail login captcha。 (它可能不是實際顯示驗證碼給你,但是這突然讓我的郵件都要經過額外的步驟。)
-2

下載sendmail的用於Windows和sendmail.ini到C:/ usr/lib中/

編輯sendmail.ini並輸入您的郵件帳戶的憑據。

您可能需要配置這兩個領域以及(或發送可能無法正常工作) [email protected] [email protected] 通過我註釋掉debug_logfile這樣我就可以看到的樣子什麼數據被髮送到我的SMTP服務器。

編輯c:\ PHP \ php.ini中

sendmail_from = [email protected]

;僅適用於Unix。您也可以提供參數(默認值:「sendmail -t -i」)。 sendmail_path = C:/usr/lib/sendmail.exe -t -i

重啓Apache 開始sendmail.exe無論是從[開始]>運行> C:/usr/lib/sendmail.exe或轉到C :/ usr/lib在Windows資源管理器中,然後DoubleClick在exe文件。

而且這個解決方案出現在Sendmail Wamp Php

我試了一下在Windows 10,現在,它與Gmail帳戶運行

補充:

打開CMD,並使用

與sendmail的守護程序/服務
sc create SendMail binpath= "C:\usr\lib\sendmail.exe" 

現在,請確保您的系統上安裝了OpenSSL,您可以從這裏下載:https://slproweb.com/products/Win32OpenSSL.html

安裝版本你需要什麼,不記得安裝「文件到Windows文件夾」。重新啓動並重試,你會解決它。

相關問題