关于php:Laravel 5.1发送电子邮件错误:stream_set_blocking()期望参数1为资源,给定null

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

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

1
2
3
4
5
6
MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.ir
MAIL_PORT=587
MAIL_USERNAME=account@mydomain.ir
MAIL_PASSWORD= ....
MAIL_ENCRYPTION=null

这在config/mail.php中:

1
'from' => ['address' =>env('MAIL_USERNAME', '[email protected]'), 'name' => 'ESET'],

和一个测试功能,用于发送简单的测试电子邮件:

1
2
3
4
5
6
7
8
public function getEm()
{
    $input = [] ;
    Mail::send('emails.simple', $input, function($message) {
            $message->to('[email protected]', 'Learning')->subject('Contact view Our Contact Form');
    });
    return 'Your message has been sent';
}

当我在localhost(在WampServer中)测试此项目时,我运行良好并发送电子邮件ok!

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

1
2
3
4
5
6
MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=587
MAIL_USERNAME=account@mydomain.ir
MAIL_PASSWORD=....
MAIL_ENCRYPTION=null

我看到这个错误:

ErrorException in StreamBuffer.php line 275:

stream_set_blocking() expects parameter 1 to be resource, null given

in StreamBuffer.php line 275 at HandleExceptions->handleError('2',
'stream_set_blocking() expects parameter 1 to be resource, null
given',
'/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)) at stream_set_blocking(null, '1') in StreamBuffer.php line 275
at Swift_Transport_StreamBuffer->_establishSocketConnection() in
StreamBuffer.php line 62 at
Swift_Transport_StreamBuffer->initialize(array('protocol' => null,
'host' => 'localhost', 'port' => '587', 'timeout' => '30', 'blocking'
=> '1', 'tls' => false, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php line 113 at
Swift_Transport_AbstractSmtpTransport->start() in Mailer.php line 79
at Swift_Mailer->send(object(Swift_Message), array()) in Mailer.php
line 395 at Mailer->sendSwiftMessage(object(Swift_Message)) in
Mailer.php line 181 at Mailer->send('emails.simple', array(),
object(Closure)) in Facade.php line 222 at
Facade::__callStatic('send', array('emails.simple', array(),
object(Closure))) in CallbackController.php line 114 at
Mail::send('emails.simple', array(), object(Closure)) in
CallbackController.php line 114 at CallbackController->getEm()


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

  • 云虚拟机环境不支持stream_socket_client函数。
  • 只需将他更改为fsockopen函数。端口为465 ssl,因为被禁止使用25。类似于$this->stream = @fsockopen($host, $this->params['port'], $errno, $errstr, $timeout)
  • 在Cloud虚拟机背景中打开fsockopen函数。
  • 对不起,我的英语不太好。