Laravel 5.5.28
我正在使用 Laravel 的按需通知将简单表单提交的电子邮件发送到中央地址。 我在这里关注了拉拉维尔文档
我在 env 文件中设置了邮件陷阱。
我的控制器中的代码:
use Notification // set at top of class
$submission = FormSubmission::create($request->all());
Notification::route('mail', 'test@test.org')
->notify(new FormSubmissionNotificiation($submission));
但是得到一个哎呀错误。 在此方法中,它在vendor/laravel/framework/src/Illuminate/Mail/MailServiceProvider.php
上失败
protected function setGlobalAddress($mailer, array $config, $type)
{
$address = Arr::get($config, $type);
if (is_array($address) && isset($address['address'])) {
$mailer->{'always'.Str::studly($type)}($address['address'], $address['name']);
}
}
它试图查找$address['name']
索引的位置。 但是我没有名字,如果我有名字,我把它放在哪里?
似乎想不通,任何帮助表示赞赏。
编辑:我尝试过另一种方式。我将一个用户添加到我的数据库中,并将Notifiable
特征添加到User
模型中,并尝试发送类似
$user->notify(new FormSubmissionNotification($submission);
,仍然得到同样的错误。
从通知文档中:
发送邮件通知时,请务必在
config/app.php
配置文件中设置name
值。此值将用于邮件通知消息的页眉和页脚。
您必须在.env
中设置MAIL_NAME
在config/mail.php
/*
|--------------------------------------------------------------------------
| 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', null), 'name' => env('MAIL_NAME', null)],
https://laravel.com/docs/5.5/mail#writing-mailables
好的,我想通了。 其实很愚蠢。
我在 .env 文件中添加了一个 MAIL_TO_ADDRESS
变量来保存我要向其发送通知的电子邮件地址,但不想直接在控制器中调用 env 文件,因此在 config/mail.php
文件中设置一个新的数组元素,如下所示
'to' => [
'address' => env('MAIL_TO_ADDRESS')
],
然后我计划像这样在控制器中使用它
Notification::route('mail', config('mail.to.address'))
->notify(new FormSubmissionNotificiation($submission));
但是,即使我直接在控制器中使用字符串中的虚拟电子邮件地址进行测试,它也将该to
变量与邮件配置文件中的电子邮件地址一起使用。 即使我没有在任何地方引用它。
一旦我从配置中删除了整个数组,它就可以工作了,同样,如果我向该数组添加一个名称,它就可以工作了。 这也阻止了我使用标准$user->notify()
,并且总是尝试使用env文件中的电子邮件地址而不是用户模型。
你必须在 config\mail 中的 'to' 数组中输入名称.php
to' => [
'address' => env('MAIL_TO_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
并在各自的模型中
public function routeNotificationForMail($notification)
{
return $this->email_address=config('mail.to.address');
}
参见拉拉维尔官方文档 laravel@8.x