简体   繁体   中英

Using gmail smtp via Laravel: [Connection timed out #110]

Before doing down vote or making it duplicate, please read the description .

I know this question is asked many times and have different solutions and I have tried all but no one has worked. This is my settings.

 MAIL_DRIVER=smtp
 MAIL_HOST=smtp.gmail.com
 MAIL_PORT=465
 MAIL_USERNAME=abc@mydomain.com
 MAIL_PASSWORD=mypassword
 MAIL_ENCRYPTION=ssl

And application is currently running on Centos 7 OS.

I always get

Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [Connection timed out #110]

at Swift_Transport_StreamBuffer->initialize(array('protocol' => 'ssl', 'host' => 'smtp.gmail.com', 'port' => '465', 'timeout' => '30', 'blocking' => '1', 'tls' => false, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php line 113

I have tried all solutions, by Replacing: smtp.gmail.com with 173.194.65.108 , or replacing smtp.gmail.com by gmail-smtp-msa.l.google.com . When I do these two steps, it got server not found error.

If I change mail driver from smtp to sendmail or mail , no error is thrown but no email is sent.

If I use ip address of smtp.gmail.com, I got same result. If I change port from 465 to 587 and encryption from ssl to tls , nothing happens.

I have also created a file in etc/gai.conf and put precedence ::ffff:0:0/96 100 . It worked one time. But later after two hours, it stopped working and started throwing same error.

I have also tried using my gmail id but all in vain.

The only solution that I couldn't be able to test is verifying httpd_can_sendmail as whenever I run command getsebool httpd_can_sendmail I get getsebool: SELinux is disabled . Is this the problem or is there any other way to get it fixed?

Here is my working mail config file:

return [
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'from' => ['address' => '*******@gmail.com', 'name' => '****'],
    'encryption' => 'tls',
    'username' => '*******@gmail.com',
    'password' => '*******',
    'sendmail' => '/usr/sbin/sendmail -bs',
];

but from your question i can see you already tried all this, so i can assume it is a network problem based on error 'connection time out', what i mean is that something is blocking your connection, it might be your firewall if you have.

so first you should check that the port 587 is open, or you can check if other application can connect with smtp.gmail.com (don't know how to do it in centOs).

And at last this might not be a problem but you have to enable 'Allow less secure apps' in your google account just for testing. (This is not permanent solution but for testing, if it works then you should enable two step verification in google account and then create new application, you can create new special password which you can use as your password for smtp server)

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