PEAR Mail unable to connect to Gmail SMTP, failed to connect to socket

后端 未结 8 1667
广开言路
广开言路 2020-12-11 00:51

Facts

I am using PEAR Mail, I want to use gmail SMTP to send a mail. I have Apache/2.4.27 (Win64) PHP/7.2.0beta3, PEAR 1.10.15, Mail 1.4.1, Net_SMT

相关标签:
8条回答
  • 2020-12-11 01:43

    In PHP 5.3, the tip given by others about "less secure apps," that need to be activated when logged into your Google Gmail account, solved all my problems.

    In PHP 7.2 I had to do more: setting 'auth' to PLAIN and then add "verify_peer" and verify_peer_name for ssl socket_options like this:

    $mail= Mail::factory('smtp', array('host' => 'ssl://smtp.gmail.com',
                                       'port' => '465',
                                       'auth' => 'PLAIN',
                                       'socket_options' => array('ssl' => array('verify_peer' => false,
                                                                                'verify_peer_name' => false)),
                                       'username' => 'someAccount@gmail.com',
                                       'password' => 'myPassword'
    ));
    
    0 讨论(0)
  • 2020-12-11 01:45

    Been Kyung-yoong is the only person to have made a meaningful contribution to solving the problem so far (+1 Been!). I can confirm his result. And I would recommend you try the same. You are currently trying to debug a rather complex stack of components:

    • Error is being thrown by PEAR smtp here
    • Which is failing when it calls Net_SMTP::connect()
    • which is a wrapper around Net_Socket::connect()
    • which is rather elaborate wrapper around fsockopen()

    Been is doing your job for you - as the person posting the question - should be creating a Minimal, Complete, and Verifiable example

    This will hopefully also provide more meaningful diagnostic information.

    The most likely reasons for this to be failing are:

    • the host you are running this on cannot route outgoing connections to the internet (but since you seem to be using a desktop PC, I would think you might have noticed this by now)
    • the code is running within a security sandbox (but MSWindows doesn't really have such things)
    • the host is unable to resolve the hostname (see first point about routing)
    • the host is able to connect but unable to verify the certificate

    Hence you might consider this more elaborate implementation of a test script:

     <?php
    
     error_reporting(E_ALL);
    
     print "DNS\n";
     var_dump(getmxrr('gmail.com',$result));
     var_dump($result);
     $use_ip=gethostbyname($result[0]);
     print "IPV4 address = $use_ip\n";
    
     print "\nIf you've got this far without errors then problem is with your SSL config\n";
     $calocns=openssl_get_cert_locations();
     if (count($calocns)) {
         print "Check you've got your cacerts deployed in one of the following locations\n";
         foreach ($calocns as $k=>$v) print "$k = $v\n";
     } else {
         print "You've not configured your openssl installation on this host\n";
     }
    
     print "\nIf all good so far, then this bit should work....\n";
     print "fsockopen\n";
     var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr, 3.0));
     var_dump($errno);
     var_dump($errstr);
    

    Which should give you a response like this:

     DNS
     bool(true)
     array(5) {
       [0]=>
       string(31) "alt1.gmail-smtp-in.l.google.com"
       [1]=>
       string(31) "alt2.gmail-smtp-in.l.google.com"
       [2]=>
       string(31) "alt4.gmail-smtp-in.l.google.com"
       [3]=>
       string(26) "gmail-smtp-in.l.google.com"
       [4]=>
       string(31) "alt3.gmail-smtp-in.l.google.com"
     }
     IPV4 address = 74.125.131.26
    
     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 = /usr/lib/ssl/cert.pem
     default_cert_file_env = SSL_CERT_FILE
     default_cert_dir = /usr/lib/ssl/certs
     default_cert_dir_env = SSL_CERT_DIR
     default_private_dir = /usr/lib/ssl/private
     default_default_cert_area = /usr/lib/ssl
     ini_cafile =
     ini_capath =
    
     If all good so far, then this bit should work....
     fsockopen
     resource(4) of type (stream)
     int(0)
     string(0) ""
    

    Given that we can't replicate your error we can't give a definitive answer what the problem is - but my guess would be that you haven't configure openSSL.

    0 讨论(0)
提交回复
热议问题