1

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
    Have you read this? http://stackoverflow.com/questions/6168882/phpmailer-to-use-gmail-as-smtp-server-could-not-connect-to-smtp-host-mailer-err – Hardy Dec 29 '13 at 17:33
  • I have read that already. The primary solution in that example was making sure extension=php_openssl.dll was active in the php.ini file. This has been done. Again, the server itself is not having a problem connecting, just PHPMailer. – user2709974 Dec 29 '13 at 17:50

2 Answers2

2

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.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • I tried changing the port to 465 for all connections attempted in my original post, with the same results. – user2709974 Dec 29 '13 at 17:47
  • And what if you remove the `$smtp->SMTPSecure` ? – Bart Friederichs Dec 29 '13 at 17:52
  • Nothing has changed. The fsockopen connects through port 465, but PHPMailer fails to connect. – user2709974 Dec 29 '13 at 17:56
  • Do not use `SMTPSecure` and connect on port 587, then you will be the same as your telnet example. – Bart Friederichs Dec 29 '13 at 17:57
  • I have already attempted using that exact code, with no success. The only difference I noticed in this example was that it defined $mail->Host twice, and it requested a different smtp server with the first definition. This is the only example I've found that does this. – user2709974 Dec 29 '13 at 18:14
  • I do get a different error code with this example - just more detailed: SMTP -> ERROR: Failed to connect to server: Connection timed out (110)SMTP Connect() failed. – user2709974 Dec 29 '13 at 18:15
  • So your example did not work for me. And this goes back to my original question. How can I connect via SSH or fsockopen if there is a firewall issue? – user2709974 Dec 29 '13 at 18:50
  • @user2709974 you can use `openssl` to test SSL links in a telnet-kind of way. It should be available on any Linux distro and the manual can help you out. – Bart Friederichs Dec 29 '13 at 18:52
  • I found the answer. It won't let me post it yet, so I will do so when I can. It ended up being a problem with the PHPMailer script. – user2709974 Dec 29 '13 at 22:23
0

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!