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

后端 未结 8 1666
广开言路
广开言路 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:31

    Few debugging steps :

    1. check phpinfo

    I recommend checking phpinfo() to check whether all modules are enabled. Check for mail, fsocketopen.

    2. Enable debug flag

    Enable debug flag to check exactly what's the problem. Like below.

    $smtp = Mail::factory('smtp', array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => '465',
            'auth' => true,
            'debug' => true,
            'pipelining' => true,
            'username' => 'xxx@gmail.com',
            'password' => 'xxx'
        ));
    

    After running above code on my machine I got follow response. Issue can be different from yours. But debug helped me. As I have 2FA enabled, it gave me error. And I got a mail also, that my login has been blocked.

    DEBUG: Recv: 220 smtp.gmail.com ESMTP s65sm4891344pfi.36 - gsmtp DEBUG: Send: EHLO localhost DEBUG: Recv: 250-smtp.gmail.com at your service, [110.227.210.84] DEBUG: Recv: 250-SIZE 35882577 DEBUG: Recv: 250-8BITMIME DEBUG: Recv: 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH DEBUG: Recv: 250-ENHANCEDSTATUSCODES DEBUG: Recv: 250-PIPELINING DEBUG: Recv: 250-CHUNKING DEBUG: Recv: 250 SMTPUTF8 DEBUG: Send: AUTH LOGIN DEBUG: Recv: 334 VsadfSFcm5hbWU6 DEBUG: Send: cGF0ZWwuZ29wYhkafdaASFnbWFpbC5jb20= DEBUG: Recv: 334 UGFzc3dvcmQ6 DEBUG: Send: OWwzMy5zaHlAbTE4 DEBUG: Recv: 534-5.7.14 Please log in via your web browser and DEBUG: Recv: 534-5.7.14 then try again. DEBUG: Recv: 534-5.7.14 Learn more at DEBUG: Recv: 534 5.7.14 https://support.google.com/mail/answer/78754 s65sm4891344pfi.36 - gsmtp DEBUG: Send: RSET DEBUG: Send: QUIT DEBUG: Recv: 250 2.1.5 Flushed s65sm4891344pfi.36 - gsmtp DEBUG: Recv: 221 2.0.0 closing connection s65sm4891344pfi.36 - gsmtp
    authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14 Please log in via your web browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/answer/78754 s65sm4891344pfi.36 - gsmtp)]
    

    Update:

    Your issue looks like PHP is not even able to connect to gmail server.

    0 讨论(0)
  • 2020-12-11 01:33

    The "Use the Gmail SMTP Server" section of this guide says you need to enable "Less secure apps".

    0 讨论(0)
  • 2020-12-11 01:33

    Your host configuration shouldn't contain the protocol. The reason it's failing is because it's probably trying to perform a DNS Lookup on ssl://smtp.gmail.com and failing.

    Change

    'host' => 'ssl://smtp.gmail.com',
    

    to

    'host' => 'smtp.gmail.com',
    
    0 讨论(0)
  • 2020-12-11 01:34

    Your code is correct

    I tried to test my gmail account. Mail sending was successful.

    Check your socket connection

    <?php
    
    error_reporting(E_ALL);
    
    var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr));
    var_dump($errno);
    var_dump($errstr);
    

    resource(4) of type (stream)

    int(0)

    string(0) ""

    0 讨论(0)
  • 2020-12-11 01:38

    Before I begin, let me preface this that there are many possibly solutions and outcomes between your server and the google server, so these may or may not work for different people.

    1) SMTP is not very secure, so Google may be rejecting your request. I had this problem 6 months ago and the solution was enabling insecure apps under 'myaccount.google.com'

    2) If that doesn't work for you then you may consider switching protocols.

    from

    'host' => 'ssl://smtp.gmail.com',

    to

    'host' => 'tls://smtp.gmail.com:587';

    0 讨论(0)
  • 2020-12-11 01:41

    When something fail and we don't know the cause we have to do debugging. So here instead of putting an answer I am requesting you to execute some tests

    1. confirm system connectivity with internet: Open cmd terminal and type

      ping smtp.gmail.com
      
    2. confirm firewall: Enter following in cmd terminal

      telnet smtp.gmail.com 465
      
    3. confirm php setup: enter php -a at cmd terminal and on php prompt execute (copy / paste and then press enter) following code.

      $result = fsockopen('ssl://smtp.gmail.com', 465, $error_no, $error_message, 5);
      if ($result === false) {
        echo "error no: $error_no error message: $error_message";
        echo print_r($result, true);
      } else {
        echo 'success';
      }
      
    4. confirm Pear Mail library and Gmail SMTP access: again on cmd and php prompt php -a execute your own code (as you posted in this thread)

    And lets know where it breaks, and what is the error. Only after that we can help

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