naspy971
naspy971

Reputation: 1381

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

I've been struggling with an issue of sending emails in production for a while now.

In local I was using mailtrap, and it worked well. But now in production I need to use gmail.

I configured the 2 step verification already. I used to get different types of errors but now I get a different one so let's focuse on this one.

So, in local, this .env configuration works, the email gets sent :

APP_NAME=Test
APP_ENV=local
APP_KEY=appkey
APP_DEBUG=true
APP_URL=http://test.test

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=192.168.10.10
DB_PORT=3306
DB_DATABASE=loveenglish
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

#MAIL_DRIVER=smtp
#MAIL_HOST=smtp.mailtrap.io
#MAIL_PORT=2525
#MAIL_USERNAME=username
#MAIL_PASSWORD=password
#MAIL_ENCRYPTION=

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=twosteppassword
MAIL_ENCRYPTION=tls

But in production, here's the .env file (It's someone else who put the app in production) :

APP_NAME=Test
APP_ENV=production
APP_KEY=basekey
APP_DEBUG=false
APP_URL=http://www.test.fr
LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=host
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=db
DB_PASSWORD=db

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=twosteppassword
MAIL_ENCRYPTION=tls

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

I get this error in my laravel.log file, a few seconds after trying to send the email :

[2018-07-20 16:01:49] production.ERROR: Connection could not be established with host smtp.gmail.com [Connection timed out #110] {"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host smtp.gmail.com [Connection timed out #110] at /homepages/25/d490854865/htdocs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:269)
[stacktrace]
#0 /homepages/25/d490854865/htdocs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(62): Swift_Transport_StreamBuffer->establishSocketConnection()
#1 /homepages/25/d490854865/htdocs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(126): Swift_Transport_StreamBuffer->initialize(Array)
#2 /homepages/25/d490854865/htdocs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(67): Swift_Transport_AbstractSmtpTransport->start()
#3 /homepages/25/d490854865/htdocs/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(464): Swift_Mailer->send(Object(Swift_Message), Array)
#4 /homepages/25/d490854865/htdocs/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(248): Illuminate\Mail\Mailer->sendSwiftMessage(Object(Swift_Message))
#5 /homepages/25/d490854865/htdocs/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): Illuminate\Mail\Mailer->send('emails.recomman...', Array, Object(Closure))

Why is it working in local env but in production it returns me an error ???

Upvotes: 0

Views: 993

Answers (1)

philip mudenyo
philip mudenyo

Reputation: 764

I fixed this by adding the below code

$transport = Swift_SmtpTransport::newInstance($smtp, $smtpPort,$security);
$transport->setUsername($username);
$transport->setPassword($password);

//****** add this to mimick localhost *****
$transport->setSourceIp('0.0.0.0');

$swift = Swift_Mailer::newInstance($transport);

Upvotes: 1

Related Questions