stream_set_blocking()期望参数1为resource,给定null

时间:2017-07-29 21:20:20

标签: php laravel smtp laravel-5.3 swiftmailer

我想通过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'),
        ],
    ],

];

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

enter image description here

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

要解决此问题,请尝试:

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

答案 2 :(得分:0)

stream_set_blocking()期望一个资源作为其第一个参数。当该资源不存在时,会发生错误。这反过来可能是因为套接字连接无法成功打开。

有问题的行可能来自AbstractSmtpTransport.php, line 113

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

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

我首先要研究协议为什么是一个空字符串,以及它应该是什么。