Laravel 5.1 发送电子邮件错误:stream_set_blocking() 期望参数 1 是资源,给定空值


Laravel 5.1 sending email error : stream_set_blocking() expects parameter 1 to be resource, null given

我正在使用Laravel 5.1,我想发送电子邮件(smtp)这是我.env配置:

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.ir
MAIL_PORT=587
MAIL_USERNAME=account@mydomain.ir
MAIL_PASSWORD= ....
MAIL_ENCRYPTION=null

这在config/mail.php

'from' => ['address' =>env('MAIL_USERNAME', 'account@mydomain.ir'), 'name' => 'ESET'],

以及发送简单测试电子邮件的测试功能:

public function getEm()
{
    $input = [] ;
    Mail::send('emails.simple', $input, function($message) {
            $message->to('some@gmail.com', 'Learning')->subject('Contact view Our Contact Form');
    });
    return 'Your message has been sent';
}

当我在本地主机(在 WampServer 中)测试这个项目时,我工作得很好并发送电子邮件正常!

但是当我想在服务器(在线)中使用它时,我会更改我的.env配置,因为我的主机支持是这样说的:

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=587
MAIL_USERNAME=account@mydomain.ir
MAIL_PASSWORD=....
MAIL_ENCRYPTION=null

我看到此错误:

StreamBuffer 中的 ErrorException.php第 275 行:

stream_set_blocking() 期望参数 1 是资源,给定空

在 StreamBuffer 中.php 第 275 行位于 HandleExceptions->handleError('2','stream_set_blocking() 期望参数 1 是资源,空给定','/home2/net45987/domains/esetn32.ir/public_html/la/user/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php','275', array('host' => 'localhost', 'timeout' => '30', 'options' =>array(), 'streamContext' => resource, 'errno' => null, 'errstr' =>null)) 在 StreamBuffer 中的 stream_set_blocking(null, '1'.php第 275 行在 Swift_Transport_StreamBuffer->_establishSocketConnection() 中流缓冲区.php第 62 行位于Swift_Transport_StreamBuffer->initialize(array('protocol' => null,"主机" => "本地主机", "端口" => "587", "超时" => "30", "阻塞"=> '1', 'tls' => false, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php 行 113 atSwift_Transport_AbstractSmtpTransport->start() 在 Mailer 中.php第 79 行在 Mailer 中的 Swift_Mailer->send(object(Swift_Message), array().php第 395 行在 Mailer->sendSwiftMessage(object(Swift_Message)) inMailer.php line 181 at Mailer->send('emails.simple', array(),对象(闭包))在立面.php第 222 行Facade::__callStatic('send', array('emails.simple', array(),object(Closure))) 在 CallbackController 中.php第 114 行位于Mail::send('emails.simple', array(), object(Closure)) inCallbackController.php 第 114 行位于 CallbackController->getEm()

你解决这个问题这么久了吗?我最近遇到了同样的问题。解决方案是:

  1. 云虚拟机环境不支持stream_socket_client功能。
  2. 只需将他更改为fsockopen function.port是465 ssl,因为25被禁止。与$this->stream = @fsockopen($host, $this->params['port'], $errno, $errstr, $timeout)相似
  3. 在云虚拟机后台打开 fsockopen 函数。

我的英语不是很好,对不起。