2017-01-02 78 views
0

由於我已經將我的網站上傳到服務器,因此我無法使用PHPMailer發送smtp郵件。它在我的本地服務器(XAMPP)上工作過。我使用GMAIL,因此我還更改了所有必需的gmail設置(即,我創建了另一個OAuth2令牌)。然而,它只是將無法正常工作......自從上傳網站後PHPMailer無法正常工作

我使用(而這也是脫機工作)的PHP代碼如下:

<?php 
/** 
* This example shows settings to use when sending via Google's Gmail servers. 
*/ 

//SMTP needs accurate times, and the PHP time zone MUST be set 
//This should be done in your php.ini, but this is how to do it if you don't have access to that 
date_default_timezone_set('Etc/UTC'); 

//Load dependencies from composer 
//If this causes an error, run 'composer install' 
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php'; 

//Load dependencies from composer 
//If this causes an error, run 'composer install' 
require 'vendor/autoload.php'; 

//Create a new PHPMailer instance 
$mail = new PHPMailerOAuth; 

//Tell PHPMailer to use SMTP 
$mail->isSMTP(); 

//Enable SMTP debugging 
// 0 = off (for production use) 
// 1 = client messages 
// 2 = client and server messages 
$mail->SMTPDebug = 0; 

//Ask for HTML-friendly debug output 
$mail->Debugoutput = 'html'; 

//Set the hostname of the mail server 
$mail->Host = 'smtp.gmail.com'; 

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
$mail->Port = 587; 

//Set the encryption system to use - ssl (deprecated) or tls 
$mail->SMTPSecure = 'tls'; 

//Whether to use SMTP authentication 
$mail->SMTPAuth = true; 

//Set AuthType 
$mail->AuthType = 'XOAUTH2'; 

//User Email to use for SMTP authentication - Use the same Email used in Google Developer Console 
$mail->oauthUserEmail = "[email protected]"; 

//Obtained From Google Developer Console 
$mail->oauthClientId = "IWONTTELLU.apps.googleusercontent.com"; 

//Obtained From Google Developer Console 
$mail->oauthClientSecret = "IWONTTELLU"; 

//Obtained By running get_oauth_token.php after setting up APP in Google Developer Console. 
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php 
// eg: http://localhost/phpmail/get_oauth_token.php 
$mail->oauthRefreshToken = "1/IWONTTELLU"; 

//Set who the message is to be sent from 
//For gmail, this generally needs to be the same as the user you logged in as 
$mail->setFrom('[email protected]', 'BLA'); 

//Set who the message is to be sent to 
$mail->addAddress('[email protected]', 'Hi'); 

//Set the subject line 
$mail->Subject = 'PHPMailer GMail SMTP test'; 

//Read an HTML message body from an external file, convert referenced images to embedded, 
//convert HTML into a basic plain-text alternative body 
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); 

//Replace the plain text body with one created manually 
$mail->AltBody = 'This is a plain-text message body'; 

//Attach an image file 
$mail->addAttachment('images/phpmailer_mini.png'); 

//send the message, check for errors 
if (!$mail->send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

?> 

當我嘗試,它首先用它上網的所有需要​​很長的時間,直到我得到一個錯誤(也嘗試使用gethostbyname('smtp.gmail.com');長話短說:也沒有工作,其次),我得到的代碼是Error: Failed to connect to server: Connection timed out (110) & SMTP connect() failed。我使用的服務由1and1提供(1und1/1 & 1)。我一直試圖解決這個問題超過4個小時,並且無法找出問題所在。不太安全的應用程序可以訪問,我也創建了一個令牌並「創建」了一個刷新令牌,如https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2。故障排除指南(https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)也無法真正幫助我,儘管我認爲它在驗證過程中遇到了問題......我也嘗試使用ssl(當然,我也更改了端口等),但它們都不起作用。

+0

我會建議在那裏添加調試代碼,並找出你是否得到一個PHP錯誤(檢查PHP日誌,如果你有)。它可能是很多東西,所以刪除明顯的東西有助於更接近解決方案。一般的超時消息沒有幫助。我也只是檢查驗證件,看看你是否得到有效的狀態碼。 – Shawn

回答

0

端口587是否仍受gmail支持?你可能想要你的SSL端口465.這篇文章可能也是有用的https://www.wpsitecare.com/gmail-smtp-settings/或可能是1 & 1被gmail阻止?看到這篇文章http://docs.mailpoet.com/article/61-sending-with-gmail-doesnt-work

+0

正如我所說,它正在離線工作......我將在明天聯繫1&1,所以我將無法確定它是否因爲他們阻塞了端口而無法工作...... – Moritz

+0

如果您在本地工作,爲什麼1&1與它有關,除非你的意思是他們提供你的互聯網連接,並且已經注意到你正試圖訪問端口587 ......我懷疑它。 – Tina

+1

不,我測試了我的所有代碼,並且測試了我創建的新令牌是否正在工作,他們做了什麼(本地主機)...我今天將我的網站上傳到了1&1,並且無法在線發送電子郵件(通過本網站)可以在本地服務器上,這就是問題所在。 – Moritz

相關問題