24

When I try to use GMail SMTP for sending email via Laravel, I encounter the following error:

Swift_TransportException

Connection could not be established with host smtp.gmail.com [Connection timed out #110]

It is the trace of the error:

...
 }
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options));
if (false === $this->_stream) {
throw new Swift_TransportException(
'Connection could not be established with host ' . $this->_params['host'] .
' [' . $errstr . ' #' . $errno . ']'...

and here are my configuration for mail:

'driver' => 'smtp',

'host' => 'smtp.gmail.com',

'port' => 587,

'from' => array('address' => '[email protected]', 'name' => 'some'),

'encryption' => 'tls',

'username' => '[email protected]',

'password' => 'mypassword',

'sendmail' => '/usr/sbin/sendmail -bs',

'pretend' => false

I use a shared host and the port 587 on localhost is open.

1

18 Answers 18

49

I had the same problem and I resolved it in this way:

'driver' => 'sendmail',

You need to change only that line.

9
  • And you used smtp of gmail? then I think the driver should be smtp, not?
    – Ahmad
    Commented Jan 23, 2015 at 18:18
  • 6
    This proceed through, but the email is not sending.
    – code-8
    Commented Nov 10, 2015 at 13:20
  • 3
    @mr5 config/email.php Commented Mar 2, 2016 at 18:24
  • 11
    Sendmail driver option send email through server default smtp. It is not send mail through google smtp.. this is not true answer. Commented Sep 10, 2016 at 5:20
  • 1
    sendmail is lib for linux system first need to activate on server side then it will works if you using a phpmailer you have option with what stmp service be useing. sendmail id lib for linux package it create stmp server on machine
    – Mr Coder
    Commented Feb 13, 2020 at 10:05
17

After doing lot of research I found this one helpful.

https://www.google.com/settings/security/lesssecureapps.

Open the above link .

Click on Enable. And save it.

Then try to send email again.

For me it worked .

9

Solved mine by changing my .env file as follows:

'driver' => 'sendmail',
3
  • did send mail??
    – Vishal B
    Commented Aug 25, 2018 at 8:16
  • 2
    sendmail is lib for linux system first need to activate on server side then it will works
    – Mr Coder
    Commented Feb 13, 2020 at 10:03
  • this does not send emails via gmail
    – loau
    Commented Jun 17 at 7:51
6

Works for me with same settings except encryption and port. Change to:

'encryption' => ssl,
'port' => 465,

Since this is only for localhost encryption line should also be environment specific. So instead above I did following:

env('MAIL_ENCRYPTION','tls'),

Now you can set this in .env file, which is environment specific and should be in .gitignore

1
  • It worked, btw I'm using a custom domain as smtp host and not google.
    – lance2k
    Commented Jan 15 at 6:44
5

Try

'encryption' => 'ssl',

'port' => 465,
5

The problem is that smtp.gmail.com is resolving an IPv6 address and that google service only listens on IPv4. What you need to do is set the source IP to ensure domains resolve as IPv4 and not IPv6.

The important method:

    ->setSourceIp('0.0.0.0')

How you might use it in code:

   $this->_transport = Swift_SmtpTransport::newInstance
        (
            'smtp.gmail.com',
            465,
            'ssl'
        )
            ->setUsername('username')
            ->setSourceIp('0.0.0.0')
            ->setPassword('password');
1
4

I got the same problem using laravel forge + digitalocean.

I find when i try telnet smtp.gmail.com 465

telnet smtp.gmail.com 465
Trying 2404:6800:4003:c00::6d...  # more than 30 sec
Trying 74.125.200.108...          # less 1 sec
Connected to smtp.gmail.com.

Maybe is IPv6 that it Connection timed out.

So,I change gai.conf to prioritize ipv4 over ipv6

vi /etc/gai.conf

#For sites which prefer IPv4 connections change the last line to
precedence ::ffff:0:0/96 100
...
#    For sites which use site-local IPv4 addresses behind NAT there is
#    the problem that even if IPv4 addresses are preferred they do not
#    have the same scope and are therefore not sorted first.  To change
#    this use only these rules:
#
scopev4 ::ffff:169.254.0.0/112  2
scopev4 ::ffff:127.0.0.0/104    2
scopev4 ::ffff:0.0.0.0/96       14
2
3

open your .env file and change this

MAIL_DRIVER=smtp

TO

MAIL_DRIVER=sendmail

this fixed my problem, hope it will help you as well.

0
1

The error might be due to 2 step verification enabled. In that case you need to create gmail app password and use this as password.

0
1

For me after trying all above solution the only thing that worked for my was Disabling My Firewall and Antivirus temporarily

0

If it is a non-Google Apps account, definitely enable access from less secure apps as suggested. If you don't do that, it won't work.

If it is a Google Apps account (ie it's a business account) then there is an admin panel that governs access. You will need to make sure it is only specifying access by authentication and not by IP, since if it is by IP your IP presumably is not on the list.

The final thing to try is using the IPv4 address of smtp.gmail.com in place of that domain name in your code. I found that mine would not connect using the domain (because that resolved to an IPv6 address) but would connect when I used the raw IP in its place.

0

I got the same problem using Swiftmailer

Anyway, a quick dirty hack you should not use would be to edit swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php. In _establishSocketConnection() line 253 replace:

$options = array();

with something like this:

$options = array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false));

This will change the ssl options of stream_context_create() (a few lines below $options):

$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, 
$errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options));

There is a dirty hack for this You can find it here

Also my ENV is set to

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
0

In your terminal use this command

sudo ufw allow in "Postfix Submission" this enable port 587 for SMTP

0

you need to create 2 factor auth and custom password in google account. Also don't forget to add custom password for every new host you are using.

0

For temporary fix you can resolved the issue by updating env file as 'driver' => 'sendmail'

0

Check your free disk space. On my case, I have 100% used.

0

I am using MAMP on MAC. I face this Issue Step to Follow to solve this problem on MAC

SMTP MAIL on MAC OS

  1. Create a file to store our credentials:
sudo vim /etc/postfix/sasl_passwd
  1. Add something like this:
smtp.gmail.com:587 [email protected]:password
  1. Now run:
sudo postmap /etc/postfix/sasl_passwd
  1. Prepare the postfix main config file:
sudo vim /etc/postfix/main.cf
  1. Add/update these lines
    relayhost=smtp.gmail.com:587
    smtp_sasl_auth_enable=yes
    smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
    smtp_use_tls=yes
    smtp_tls_security_level=encrypt
    tls_random_source=dev:/dev/urandom
    smtp_sasl_security_options = noanonymous
    smtp_always_send_ehlo = yes
    smtp_sasl_mechanism_filter = plain
  1. Stop/Start the service
sudo postfix stop
sudo postfix start
  1. Check the queue for any errors
mailq

Your .env configuration should look like this

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME="[email protected]"
MAIL_PASSWORD="password"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
0

modify .env MAIL_MAILER=sendmail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.