【问题标题】:stream_set_blocking() expects parameter 1 to be resource, null givenstream_set_blocking() 期望参数 1 是资源,给定 null
【发布时间】:2018-01-05 17:49:48
【问题描述】:

我想使用 Laravel 5.4 从在线服务器发送电子邮件。我不知道问题出在我的代码还是服务器上。我从 easyname.com 托管。

这是我的 .env 文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.easyname.com
MAIL_PORT=465
MAIL_USERNAME=info@mydomain.com
MAIL_PASSWORD=mypasword
MAIL_ENCRYPTION=

这是我的 config/mail.php 文件

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
    |            "sparkpost", "log", "array"
    |
    */

    'driver' => env('MAIL_DRIVER', 'smtp'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Address
    |--------------------------------------------------------------------------
    |
    | Here you may provide the host address of the SMTP server used by your
    | applications. A default option is provided that is compatible with
    | the Mailgun mail service which will provide reliable deliveries.
    |
    */

    'host' => env('MAIL_HOST', 'smtp.easyname.com'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */

    'port' => env('MAIL_PORT', 465),

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'info@mydomain.com'),
        'name' => env('MAIL_FROM_NAME', 'My Name'),
    ],

    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */

    'encryption' => env('MAIL_ENCRYPTION', ''),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */

    'username' => env('MAIL_USERNAME'),

    'password' => env('MAIL_PASSWORD'),

    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -bs',

    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

当我尝试运行代码时,它给了我以下错误。我不知道我在哪里犯了错误。

【问题讨论】:

    标签: php laravel smtp laravel-5.3 swiftmailer


    【解决方案1】:

    我通过将 .env 文件和 mail.php 文件中的 MAIL_DRIVER 从 MAIL_DRIVER=smtp 更改为 MAIL_DRIVER=mail 解决了这个问题。还将 MAIL_ENCRYPTION 从 MAIL_ENCRYPTION= 更改为 MAIL_ENCRYPTION=ssl

    【讨论】:

      【解决方案2】:

      要解决此问题,请尝试:

      1. 云虚拟机环境不支持stream_socket_client功能。
      2. 只需将其更改为 fsockopen 函数即可。端口是 465 ssl,因为 25 将被禁止。类似于$this-&gt;stream = @fsockopen($host, $this-&gt;params['port'], $errno, $errstr, $timeout)
      3. 在云虚拟机后台打开 fsockopen 函数。

      【讨论】:

        【解决方案3】:

        stream_set_blocking() 需要一个资源作为其第一个参数。当该资源不存在时,您的错误就会发生。这又可能是因为套接字连接无法成功打开。

        有问题的线路很可能来自AbstractSmtpTransport.php, line 113

        Swift_Transport_StreamBuffer->initialize(
            ['protocol'=>'', 'host'=>'smtp.easyname.com', 'port'=>465...]
        )
        

        我不熟悉这个库,但我敢打赌这应该返回一个打开的套接字,但它失败了,null 被返回,并且库在使用stream_set_blocking() 中的套接字之前从未检查过是否成功/p>

        我会首先调查为什么协议是一个空字符串,以及它应该是什么。

        【讨论】:

          猜你喜欢
          • 2016-07-07
          • 1970-01-01
          • 2018-09-28
          • 2013-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-23
          • 2021-01-02
          相关资源
          最近更新 更多