dronthpi05943
2018-02-20 00:57 阅读 412
已采纳

[SMTP:无法连接套接字:fsockopen():无法连接到ssl://smtp.gmail.com:465(未知错误)(代码:-1,响应:)

I have been troubleshooting this issue for the last two months and have gotten no results on how to fix this. I am using PHP 5.6.3 with PEAR 1.10.1, the emailing pages in question worked fine with our 3-party emailing software on our local server only designed to use port 25 for SMTP. Now the PHP pages do reference an XML template that contains all the host, password, and username information. I want to switch to using google email servers with SSL. I implemented the changes provided by my email administrator and change the MX records accordingly. It was functioning properly for two weeks. After that I was getting the following error

"Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )]".

I have changed the code several times, I removed the ssl://, I have changed the protocol type to TLS with the port number 587, etc. Nothing was working! I reached out to the contractor to construct a simple hard-coded page that just said hello world using the gmail server configuration. He refused and made myself make a simple php page, mind you I had zero background in PHP programming until now, so on a side note entirely happy that this was thrown my way, but regardless I did complete the task. I have a page that sends a simple message, using the smtp server with the account desired using the PHPMailer library. (See the code below)

 <?php  


require_once 'C:\Webpage\PHPMailer-5.2-stable/PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->SMTPDebug   = 4; // 2 to enable SMTP debug information


$mail->Host = 'smtp.gmail.com';
$mail->Username = 'username@gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure ='ssl';
$mail->Port = 465;

/*$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        //'allow_self_signed' => true
    )
);*/

$mail->From = 'XXXXt@abc.com';
$mail->FromName = 'Example';
$mail->addReplyTo('testing@abc.com','Example');
$mail->AddAddress('user1@xyz.com', 'John Doe');


$mail->Subject = 'Hello World';
$mail->Body ='A test email!';
$mail->AltBody = 'A test email!';



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

Now I know this is not using PEAR, but from this I found out some interesting information that I think is related. The code only works if the line with

 $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            //'allow_self_signed' => true
        )
);

is not commented out it works, but when it is commented out, I receive an error regarding this.

Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [C:\Webpage\PHPMailer-5.2-stable\class.smtp.php line 299]

So I googled this error and was lead to the php.ini file to change the openssl.cafile and the openssl.capath values. I entered in the download CA certificates and entered the correct pathway for the values, but still it is not working. Am I placing these in the wrong area? Or is there an easier way to fix this issue using the PEAR library? Please any help on this subject will be appreciated!:)

UPDATE(02/20/2018):

I have downloaded the bundle and set the pathway to it's correct location,curl.cainfo ="C:\OpenSSL-Win64\bin\PEM\cacert.pem",openssl.cafile="C:\OpenSSL-Win64\bin\PEM\cacert.pem". I still am getting the error I ran the following check of the ssl locations to see if it was using the php.ini file and got the following.

    <?php
 error_reporting(E_ALL);

print "
If you've got this far without errors then problem is with your SSL config
";
 $calocns=openssl_get_cert_locations();
 if (count($calocns)) {
     print "Check you've got your cacerts deployed in one of the following locations
";
     foreach ($calocns as $k=>$v) print "$k = $v
";
 } else {
     print "You've not configured your openssl installation on this host
";
 }
$calocns=openssl_get_cert_locations();
//var_dump(openssl_get_cert_locations());


?>

Result:

    If you've got this far without errors then problem is with your SSL config Check you've got your cacerts deployed in one of the following locations default_cert_file = f:epo\winlibs_openssl_vc11_x86/cert.pem default_cert_file_env = SSL_CERT_FILE 
    default_cert_dir = f:epo\winlibs_openssl_vc11_x86/certs 
    default_cert_dir_env = SSL_CERT_DIR 
    default_private_dir = f:epo\winlibs_openssl_vc11_x86/private
 default_default_cert_area = f:epo\winlibs_openssl_vc11_x86 
    ini_cafile = 
    ini_capath = 

I seem to be doing this wrong or need to know how to change the locations because those locations are not existent on my computer and I don't understand how it goes to those by default when I changed the pathway in the configuration file itself. Any thoughts?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongqu9917 dongqu9917 2018-02-20 03:04

    Obviously it's still the certificate issue. Try using the latest CA certificates extracted from Mozilla. Simply download it, place it somewhere or replace your current cacert.perm, add the path to your certificate to openssl.cafile and curl.cainfo in your php.ini, and then try again. You might want to comment openssl.capath for now.

    You can read more about issues related to CA certificate in PHPMailer Troubleshooting Wiki.


    Update for 02/20/2018:

    The curl.cainfo and openssl.cafile paths looks fine, assuming that is the correct path to the cacert.pem you downloaded. However your ini_cafile is empty when you run openssl_get_cert_locations(). It should reflect whatever you set in your openssl.cafile. You will need to restart your web server (Apache/nginx) after making changes to your php.ini to take effect. After restarting check the openssl.cafile value by running phpinfo(), and make sure it is correct.

    I'm not sure why the default cert directory is like that. Maybe a previous installation of Open SSL? Either way, it's an non-existent default path for me as well but it doesn't matter since my ini_cafile shows the same value I defined in openssl.cafile. openssl_get_cert_locations() shows the list of places it will be looking for the certificates, so if the correct one is among the them, I think it should be fine.

    点赞 评论 复制链接分享

相关推荐