SMTP GMAIL Connection

20,359

Solution 1

Your server cannot connect to smtp.gmail.com on port 587. I have the same problem from a testing tool:

Resolving hostname...
Connecting...
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
Message sending failed.

From my local machine, it works perfectly:

Trying 74.125.195.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP be3sm8900765wib.21 - gsmtp

Maybe there are temporary problems at Google from the US (I am in Germany here) or something like this. There is no obvious mistake in your code. You can only try it again later or with another SMTP server.

Furthermore, you can try to connect with 74.125.195.108 directly (just deactivate SSL certificate validation).


EDIT: Just also try tls://smtp.gmail.com as host.

Solution 2

Adjust the authentication protocol and port number in your code:

// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Updated Settings
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 465;  

I've found that PHPMailer, when set to use Gmail SMTP servers with TLS authentication protocol and port number 587, simply doesn't work. However, I've never had a problem using SSL/465.

Google SMTP Settings

Difference Between TLS and SSL

Share:
20,359
Amirh. LGT
Author by

Amirh. LGT

Updated on February 11, 2020

Comments

  • Amirh. LGT
    Amirh. LGT over 4 years

    I can't connect to SMTP GMAIL with PHPMailer.

    here is error :

    Error on Jun 25, 2015 22:54PM - stream_socket_client(): unable to connect to smtp.gmail.com:587 (Connection timed out) in /home/amiroper/public_html/beporsbedoon/app/helpers/phpmailer/smtp.php on line 222

    and this is my code :

    $this->_mail->isSMTP();
    $this->_mail->Host = "smtp.gmail.com";
    $this->_mail->SMTPAuth = true;
    $this->_mail->Username = "[email protected]";
    $this->_mail->Password = "*********";
    $this->_mail->SMTPSecure = "tls";
    $this->_mail->Port = "587";
    $this->_mail->SMTPDebug = 4;
    $this->_mail->From = "AmirOperator";
    $this->_mail->FromName = '[email protected]';
    $this->_mail->addAddress("[email protected]", "test");
    $this->_mail->isHTML(true);
    $this->_mail->Subject = 'Registration confirm';
    $this->_mail->Body    = 'Thank you for registering to activate your account please click on this link. ".DIR."account/activate/$id/$activasion"';
    $this->_mail->AltBody = 'Thank you for registering to activate your account please click on this link. ".DIR."account/activate/$id/$activasion"';
    
    if(!$this->_mail->send()) {
        $data['mailsent'] = false;
    } else {
        $data['mailsent'] = true;
    }
    

    the php code is wrong or this is connection problem

  • Amirh. LGT
    Amirh. LGT almost 9 years
    thanks, I'm from Iran. May in iran we have temporary problems with smtp.gmail.com, but with 74.125.195.108 I can connect.
  • Synchro
    Synchro almost 9 years
    That means you have a DNS problem.
  • Richard
    Richard almost 9 years
    @Synchro: I do not think so, because even the test page failed. Maybe this is a problem with SSL/TLS support of Google. The testing tool explicitly says, that smtp.gmail.com has been resolved.
  • Richard
    Richard almost 9 years
    @amiroperator: Just being careful with the fix IP address as it can be changed or being loadbalanced. Have you tried with the tls:// prefix and hostname also?
  • Synchro
    Synchro almost 9 years
    You can use the prefix or the SMTPSecure property, it makes no difference which you use.