在 Lumen 中发送邮件

Sending mail in Lumen

我阅读了很多关于如何使用 Lumen 发送邮件的指南和问答。我已经尝试了很多这些建议。

但是,我仍然得到这个错误:

(1/1) FatalThrowableError
Type error: Too few arguments to function Illuminate\Support\Manager::createDriver(), 0 passed in /var/www/monitor/vendor/illuminate/support/Manager.php on line 88 and exactly 1 expected

完整堆栈跟踪 here

这是我的控制器:

use Illuminate\Support\Facades\Mail;

public function check() {
    $response = $this->getResponse();
    if ($response) {
        if ($this->isAlive($response->state)) {
            $user = new \stdClass();
            $user->email = '****@gmail.com';
            $user->name = 'Albert';
            Mail::raw('test', function($mail) use ($user) {
                $mail->to($user->email, $user->name)->subject('Test Subject');
            });
            // I've also tried Mail::send() but no luck
            echo 'System is fine';
        } else {
            echo 'System has issues';
        }
    } else {
        echo 'Error connecting';
    }
}

我已取消注释并将以下行添加到我的 bootstrap/app.php:

$app->withFacades();
$app->register(App\Providers\AppServiceProvider::class);
$app->register(Illuminate\Mail\MailServiceProvider::class);

我的 .env 文件中有以下内容:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mygmailaddress@gmail.com
MAIL_PASSWORD=mygmailpassword
MAIL_ENCRYPTION=tls

我是不是漏掉了什么?

问题是邮件管理器依赖邮件配置,Lumen从5.1开始默认不包含邮件配置。如果您使用的是 Lumen > 5.1,则需要添加自己的邮件配置文件,并更新 bootstrap 文件以加载配置文件。

首先,在 app 目录旁边创建一个 config 目录。

接下来,在新的 config 目录中添加一个 mail.php 文件。您可以从与您正在使用的 Lumen 版本匹配的默认 Laravel 安装中复制内容(例如,如果您使用的是 Lumen 5.4,只需从 Laravel 5.4 复制 mail.php 配置文件).

最后,在您的 bootstrap/app.php 文件中,加载配置文件:

$app->configure('mail');

为了安全起见,我会在您注册邮件服务提供商之前加入这一行。