一段时间以来,我一直在努力在生产环境中发送电子邮件。
在本地,我使用邮件陷阱,并且效果很好。但是现在在生产中,我需要使用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))
为什么它在本地环境中工作,但在生产环境中却返回错误?
答案 0 :(得分: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);