Laravel 5.5.28
I'm using a Laravel's On Demand Notification to send out an email of a simple form submission to a central address. I have followed the laravel docs here
I have mailtrap set up in the env file.
The code in my controller:
use Notification // set at top of class
$submission = FormSubmission::create($request->all());
Notification::route('mail', 'test@test.org')
->notify(new FormSubmissionNotificiation($submission));
but get a whoops error. It fails on the vendor/laravel/framework/src/Illuminate/Mail/MailServiceProvider.php
in this method
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']);
}
}
where it's trying to find the $address['name']
index. But I don't have a name, and if I did, where do I put it?
Can't seem to figure it out, any help appreciated.
EDIT:
I've tried this a different way. I added in a user to my database, and added the Notifiable
trait onto the User
model and attempted to send out a notification like
$user->notify(new FormSubmissionNotification($submission);
and still got the same error.
OK, I figured this out. Pretty stupid actually.
I added in a MAIL_TO_ADDRESS
variable to my .env file to hold the email address I wanted to send the notifications to, but didn't want to call the env file directly in the controller so set up a new array element in the config/mail.php
file like this
'to' => [
'address' => env('MAIL_TO_ADDRESS')
],
Then I planned to use that in the controller like this
Notification::route('mail', config('mail.to.address'))
->notify(new FormSubmissionNotificiation($submission));
But even while I was testing using a dummy email address in a string directly in the controller, it was using that to
variable with the email address from the mail config file. Even though I wasn't referencing it anywhere.
Once I deleted that whole array from the config it worked, and similarly, if I add in a name to that array, it works. That also stopped me using the standard $user->notify()
as well and was always trying to use the email address from the env file rather than the User model.