PHPMailer can't connect to Gmail

北慕城南 提交于 2019-12-11 04:18:27

问题


So I've been scouring the web on this issue, and almost every example was condemned as a firewall or server related problem. From what I can tell, my server is connecting just fine to gmail, but PHPMailer still fails to connect. Here is my PHP:

require_once($_SERVER['DOCUMENT_ROOT']."/phpmailer/class.phpmailer.php");        
$host = "smtp.gmail.com";
$port = "587";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
    echo "($errno) $errstr";
} else {
    echo 'Connected through fsocketopen.<br />';
}

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail@gmail.com";
$mail->Password = "********";
$mail->Port = "587"; 

The credentials are not shown, but are 100% correct. Here are the results:

Connected through fsocketopen.
Mailer Error: SMTP Connect() failed.

As you can see, the server is allowing a connection to gmail through fsocketopen, but PHPMailer will not connect. I've even accessed my server through SSH, and received the following response:

-bash-4.1$ telnet smtp.gmail.com 587 Trying 2607:f8b0:400d:c02::6d... Connected to smtp.gmail.com. Escape character is '^]'. 220 mx.google.com ESMTP g1sm52568728qec.9 - gsmtp

So two tests verify connection between my server and Gmail is available. So now I'm left to assume that there is a problem with my PHPMailer. I've scanned through the class.phpmailer.php file, but I just don't know enough about it to see where there would be a problem. Any help is appreciated!


回答1:


I reckon this is the problem:

$mail->SMTPSecure = "ssl";

you try to use SSL, yet in your telnet example, you don't. You should use port 465 for secure SMTP, or disable SMTPSecure.

(Note SMTPAuth and SMTPSecure are different concepts. SMTPAuth makes sure you are that you say you are. SMTPSecure encrypts the communication channel.)

Update, this code tested and works. I also tried the non-SSL'ed version, but it seems Google doesn't allow plaintext SMTP anymore.

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";  
$mail->Username = "sender@example.com";       
$mail->Password = "******";
$mail->Port = 465;                // note the ommission of quotes

$mail->AddAddress("receiver@example.com");

$mail->Subject = "test";
$mail->MsgHTML("<b>test</b>");
$mail->Send();

If it doesn't, you might have some firewalling enabled.




回答2:


So it appears as though the PHPMailer Script I had installed on the server was not able to handle third party relays - well at least not this one. I had already tried uploading the latest version of class.phpmailer.php, but in this case, I downloaded the entire package from SourceForge and reuploaded all files to the server. This did the trick. Lesson learned!



来源:https://stackoverflow.com/questions/20828008/phpmailer-cant-connect-to-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!