简体   繁体   中英

PHPMailer giving “SMTP Connection failed” error message

I have the following code:

require 'bin\PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->From = "[an e-mail address]@gmail.com";
$mail->AddAddress("[an e-mail address]@gmail.com");

$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

$mail->AltBody = 'sprava';

if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent";
}

And here is the response:

    2014-06-11 15:46:34 SERVER 
-> CLIENT: 220 mx.google.com ESMTP v47sm60079970eel.22 - gsmtp 2014-06-11 15:46:34 CLIENT 
-> SERVER: EHLO localhost 2014-06-11 15:46:34 SERVER 
-> CLIENT: 250-mx.google.com at your service, [95.103.53.55] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250 CHUNKING 2014-06-11 15:46:34 CLIENT 
-> SERVER: STARTTLS 2014-06-11 15:46:34 SERVER 
-> CLIENT: 220
    2.0.0 Ready to start TLS Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:\dev\localhost_www\bin\class.smtp.php on line 262 2014-06-11 15:46:34 CLIENT 
-> SERVER: QUIT 2014-06-11 15:46:44 SERVER 
-> CLIENT: 2014-06-11 15:46:44 SMTP ERROR: QUIT command failed: SMTP connect() failed. Mailer Error: SMTP connect() failed.

How can I fix that? I tried that thing with php.ini file but without success.

The key to the issues this this part of the error:

-> CLIENT: 220 2.0.0 Ready to start TLS Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:\\dev\\localhost_www\\bin\\class.smtp.php on line 262 2014-06-11 15:46:34 CLIENT

That means that your local PHP setup does not have php_openssl.dll enabled. You can enable that by going into your php.ini & finding a line that looks like this:

; extension=php_openssl.dll

And uncommenting it:

extension=php_openssl.dll

Save that, restart Apache & all should be good.

And if you want to make sure your are editing the correct php.ini file, create a file named phpinfo.php in your web root. And then place the following command in there:

phpinfo();

Then load phpinfo.php via a web browser like this; change http://localhost/ to match your server URL:

http://localhost/phpinfo.php

When that page loads, it will show you all of your server's PHP settings. Look for the line that reads Loaded Configuration File and change the php.ini that is loaded there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM