在 Laravel 8 中发送不带环境变量的电子邮件

Send email without env variables in Laravel 8

Laravel 版本 - 8

我想做什么?

发送没有环境变量的电子邮件。

错误详情

Connection could not be established with host mailhog :stream_socket_client(): php_network_getaddresses: getaddrinfo failed: No such host is known.

我错过了什么吗?

代码

config('MAIL_DRIVER', 'smtp');
config('MAIL_USERNAME', "email username");
config('MAIL_HOST', "smtp.gmail.com");
config('MAIL_PASSWORD', "password");
config('MAIL_PORT', "port");
config('MAIL_ENCRYPTION', "tls");

$invitedUser = new Admin();
$invitedUser->email = "recipient email address";
$invitedUser->notify(new TestingNotification());

您的配置似乎有问题。该错误提到 mailhog 作为 host,但这可能不是您想要的。

也许您将 config('MAIL_HOST', "smtp.gmail.com"); 之类的东西编写为伪代码,但通常您会在 mail.php 配置文件中进行设置。例如:

'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.gmail.com'),`.
            ...
        ]
]
config(['mail.mailers.smtp.host' => "host"]);
config(['mail.mailers.smtp.encryption' => "tls"]);
config(['mail.mailers.smtp.username' => "username"]);
config(['mail.mailers.smtp.password' => "password"]);
config(['mail.mailers.smtp.port' => "port"]);
config(['mail.mailers.smtp.from' => "from"]);


$invitedUser = new Admin();
$invitedUser->email = "recipient";
$invitedUser->notify(new TestingNotification());