【问题标题】:production.ERROR: Connection could not be established with host smtp.gmail.com [Connection timed out #110]production.ERROR:无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]
【发布时间】:2018-12-29 01:27:32
【问题描述】:

一段时间以来,我一直在努力解决在生产环境中发送电子邮件的问题。

在本地,我使用的是 mailtrap,它运行良好。但现在在生产中我需要使用 gmail。

我已经配置了两步验证。我曾经遇到过不同类型的错误,但现在我遇到了不同类型的错误,所以让我们专注于这个。

所以,在本地,这个 .env 配置有效,电子邮件被发送:

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
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=twosteppassword
MAIL_ENCRYPTION=tls

但在生产中,这是 .env 文件(将应用程序投入生产的是其他人):

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
MAIL_USERNAME=myemail@gmail.com
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}"

在尝试发送电子邮件几秒钟后,我的 laravel.log 文件中出现此错误:

[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))

为什么它在本地环境中工作但在生产中它返回一个错误???

【问题讨论】:

  • 也许您的主机在 587 上的出站连接被阻止?
  • 在 ssl 465 上相同。Mailtrap 也在生产中工作。只有gmail不起作用....
  • 嗯,是连接错误,所以看起来你的服务器无法连接到gmail,并不是真正的编程问题。,

标签: laravel smtp gmail swiftmailer


【解决方案1】:

我通过添加以下代码解决了这个问题

$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);

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 2011-02-13
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多