由於我已經將我的網站上傳到服務器,因此我無法使用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(當然,我也更改了端口等),但它們都不起作用。
我會建議在那裏添加調試代碼,並找出你是否得到一個PHP錯誤(檢查PHP日誌,如果你有)。它可能是很多東西,所以刪除明顯的東西有助於更接近解決方案。一般的超時消息沒有幫助。我也只是檢查驗證件,看看你是否得到有效的狀態碼。 – Shawn