Geo*_*san 2 php email laravel lumen
收到错误 InvalidArgumentException 无法使用 smtp 驱动程序在 Lumen 上解析 [Illuminate\\Mail\\TransportManager] 的 NULL 驱动程序。我按照lumen文档中设置邮件的所有步骤进行操作。这是我的环境
\n\nMAIL_DRIVER=smtp\nMAIL_HOST=domain.com\nMAIL_PORT=465\nMAIL_USERNAME=username@domain.com\nMAIL_PASSWORD=password\nMAIL_ENCRYPTION=tls\nMAIL_FROM_ADDRESS=no-reply@username@domain.com\nMAIL_FROM_NAME="Company name"\nRun Code Online (Sandbox Code Playgroud)\n\n将 laravel 存储库中的 mail.php 添加到 lumen.../config 文件夹中
\n\n<?php\n\nreturn [\n\n /*\n |--------------------------------------------------------------------------\n | Default Mailer\n |--------------------------------------------------------------------------\n |\n | This option controls the default mailer that is used to send any email\n | messages sent by your application. Alternative mailers may be setup\n | and used as needed; however, this mailer will be used by default.\n |\n */\n\n \'default\' => env(\'MAIL_MAILER\', \'smtp\'),\n\n /*\n |--------------------------------------------------------------------------\n | Mailer Configurations\n |--------------------------------------------------------------------------\n |\n | Here you may configure all of the mailers used by your application plus\n | their respective settings. Several examples have been configured for\n | you and you are free to add your own as your application requires.\n |\n | Laravel supports a variety of mail "transport" drivers to be used while\n | sending an e-mail. You will specify which one you are using for your\n | mailers below. You are free to add additional mailers as required.\n |\n | Supported: "smtp", "sendmail", "mailgun", "ses",\n | "postmark", "log", "array"\n |\n */\n\n \'mailers\' => [\n \'smtp\' => [\n \'transport\' => \'smtp\',\n \'host\' => env(\'MAIL_HOST\', \'smtp.mailgun.org\'),\n \'port\' => env(\'MAIL_PORT\', 587),\n \'encryption\' => env(\'MAIL_ENCRYPTION\', \'tls\'),\n \'username\' => env(\'MAIL_USERNAME\'),\n \'password\' => env(\'MAIL_PASSWORD\'),\n \'timeout\' => null,\n \'auth_mode\' => null,\n ],\n\n \'ses\' => [\n \'transport\' => \'ses\',\n ],\n\n \'mailgun\' => [\n \'transport\' => \'mailgun\',\n ],\n\n \'postmark\' => [\n \'transport\' => \'postmark\',\n ],\n\n \'sendmail\' => [\n \'transport\' => \'sendmail\',\n \'path\' => \'/usr/sbin/sendmail -bs\',\n ],\n\n \'log\' => [\n \'transport\' => \'log\',\n \'channel\' => env(\'MAIL_LOG_CHANNEL\'),\n ],\n\n \'array\' => [\n \'transport\' => \'array\',\n ],\n ],\n\n /*\n |--------------------------------------------------------------------------\n | Global "From" Address\n |--------------------------------------------------------------------------\n |\n | You may wish for all e-mails sent by your application to be sent from\n | the same address. Here, you may specify a name and address that is\n | used globally for all e-mails that are sent by your application.\n |\n */\n\n \'from\' => [\n \'address\' => env(\'MAIL_FROM_ADDRESS\', \'hello@example.com\'),\n \'name\' => env(\'MAIL_FROM_NAME\', \'Example\'),\n ],\n\n /*\n |--------------------------------------------------------------------------\n | Markdown Mail Settings\n |--------------------------------------------------------------------------\n |\n | If you are using Markdown based email rendering, you may configure your\n | theme and component paths here, allowing you to customize the design\n | of the emails. Or, you may simply stick with the Laravel defaults!\n |\n */\n\n \'markdown\' => [\n \'theme\' => \'default\',\n\n \'paths\' => [\n resource_path(\'views/vendor/mail\'),\n ],\n ],\n\n];\nRun Code Online (Sandbox Code Playgroud)\n\n将以下内容添加到我的 bootstrap 中的 app.php 中
\n\n$app->register(Illuminate\\Mail\\MailServiceProvider::class);\n$app->configure(\'mail\');\n$app->configure(\'services\');\n$app->alias(\'mailer\', Illuminate\\Mail\\Mailer::class);\n$app->alias(\'mailer\', Illuminate\\Contracts\\Mail\\Mailer::class);\n$app->alias(\'mailer\', Illuminate\\Contracts\\Mail\\MailQueue::class);\nRun Code Online (Sandbox Code Playgroud)\n\n发送电子邮件的实现
\n\npublic function testMail()\n {\n Mail::to(\'username2@domain.com.com\')->send(new MailableClass(\'George\',\'Kindly note that your email has been sent\'));\n }\nRun Code Online (Sandbox Code Playgroud)\n\nMailableClass 只是一个扩展 Mailable 来构建视图的类
\n\n我还对配置(\'mail\')进行了数据转储,它返回了这个
\n\narray:4 [\xe2\x96\xbc\n "default" => "smtp"\n "mailers" => array:7 [\xe2\x96\xbc\n "smtp" => array:8 [\xe2\x96\xbc\n "transport" => "smtp"\n "host" => "username@domain.com"\n "port" => "465"\n "encryption" => "tls"\n "username" => "username@domain.com"\n "password" => "password"\n "timeout" => null\n "auth_mode" => null\n ]\n "ses" => array:1 [\xe2\x96\xb6]\n "mailgun" => array:1 [\xe2\x96\xb6]\n "postmark" => array:1 [\xe2\x96\xb6]\n "sendmail" => array:2 [\xe2\x96\xb6]\n "log" => array:2 [\xe2\x96\xb6]\n "array" => array:1 [\xe2\x96\xb6]\n ]\n "from" => array:2 [\xe2\x96\xb6]\n "markdown" => array:2 [\xe2\x96\xb6]\nRun Code Online (Sandbox Code Playgroud)\n\n我检查照亮/邮件中的 Manager.php 和 TransportManager.php 文件\n当我将驱动程序函数的驱动程序值设置为“smtp”时,我能够传递给 TransportManager.php 的 createSmtpDriver() 。您可以在这里查看相应的代码...
\n\n /**\n * Get a driver instance.\n *\n * @param string $driver\n * @return mixed\n *\n * @throws \\InvalidArgumentException\n */\n public function driver($driver = "smtp") //originally $driver = null\n {\n $driver = $driver ?: $this->getDefaultDriver();\n\n if (is_null($driver)) {\n throw new InvalidArgumentException(sprintf(\n \'Unable to resolve NULL driver for [%s].\', static::class\n ));\n }\n\n // If the given driver has not been created before, we will create the instances\n // here and cache it so we can return it next time very quickly. If there is\n // already a driver created by this name, we\'ll just return that instance.\n if (! isset($this->drivers[$driver])) {\n $this->drivers[$driver] = $this->createDriver($driver);\n }\n\n return $this->drivers[$driver];\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我在 createSmtpDriver() 中做了一个 dd
\n\n protected function createSmtpDriver()\n {\n $config = $this->app->make(\'config\')->get(\'mail\');\n **dd($config);**\n\n // The Swift SMTP transport instance will allow us to use any SMTP backend\n // for delivering mail such as Sendgrid, Amazon SES, or a custom server\n // a developer has available. We will just pass this configured host.\n $transport = new SmtpTransport($config[\'host\'], $config[\'port\']);\n\n if (isset($config[\'encryption\'])) {\n $transport->setEncryption($config[\'encryption\']);\n }\n\n // Once we have the transport we will check for the presence of a username\n // and password. If we have it we will set the credentials on the Swift\n // transporter instance so that we\'ll properly authenticate delivery.\n if (isset($config[\'username\'])) {\n $transport->setUsername($config[\'username\']);\n\n $transport->setPassword($config[\'password\']);\n }\n\n // Next we will set any stream context options specified for the transport\n // and then return it. The option is not required any may not be inside\n // the configuration array at all so we\'ll verify that before adding.\n if (isset($config[\'stream\'])) {\n $transport->setStreamOptions($config[\'stream\']);\n }\n\n return $transport;\n }\n\nRun Code Online (Sandbox Code Playgroud)\n\n这个 dd 的结果基本上是上面 config(\'mail\') 中的结果,从中我们可以看到 config[\'host\'], config[\'port\'] ..etc 不会返回任何内容,因为它们在邮件程序中,所以我认为应该是 config[\'mailer\'][\'host\'] 而不是......但这些都是框架文件,所以......
\n\n我将非常感谢您提供的帮助来解决这个问题。我完全不知道我做错了什么。谢谢
\n\n哦对了忘了我也安装了..
\n\ncomposer require guzzlehttp/guzzle\nRun Code Online (Sandbox Code Playgroud)\n\n谢谢
\n所以我基本上将从 Laravel 存储库获得的 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.mailgun.org'),
/*
|--------------------------------------------------------------------------
| 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', 587),
/*
|--------------------------------------------------------------------------
| 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', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
/*
|--------------------------------------------------------------------------
| 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', 'tls'),
/*
|--------------------------------------------------------------------------
| 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'),
],
],
];
Run Code Online (Sandbox Code Playgroud)
现在可以了