2017-01-02 72 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

相关问题