1

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.

John Halsey
  • 1,930
  • 3
  • 19
  • 41
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – aynber Jan 08 '18 at 16:40
  • If you don't have a name, then take it out, and put `null` or `''` to pass in an empty string. – aynber Jan 08 '18 at 16:41
  • Take it out of where? – John Halsey Jan 08 '18 at 16:53

4 Answers4

0

From the notifications docs:

When sending mail notifications, be sure to set the name value in your config/app.php configuration file. This value will be used in the header and footer of your mail notification messages.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Yes, this has a value already `'name' => env('APP_NAME', 'Laravel'),` I have a value for it in my env file. – John Halsey Jan 08 '18 at 16:47
  • @JohnHalsey try to put `dd($config, $type)` to the `setGlobalAddress()` method to see what exactly config Laravel tried to use. Also, if you removed this value recently, try to clear config cache with `php artisan config:clear` – Alexey Mezenin Jan 08 '18 at 16:53
  • @alexey-mezenis goof idea thanks, it dumps the `$config` object and the type `mail` which looks fine. I also dumped `$address` in there and it is an array with both `address` and `name` keys set with values. – John Halsey Jan 08 '18 at 20:59
  • @JohnHalsey what exactly does `dd($config, $type)` show? – Alexey Mezenin Jan 08 '18 at 21:03
  • OK, i loops over a few times before it breaks. It loops over `from` and `reply_to` but breaks on `to` ``` – John Halsey Jan 08 '18 at 21:31
  • It's too long to post into a comment. But it loops over `from` and `reply_to` as the 'type' without problem, but breaks on `to`. The `to` array has just the address in it, no name which is obviously why it breaks. Looks like a bug to me. – John Halsey Jan 08 '18 at 21:39
  • @JohnHalsey please show the `FormSubmissionNotificiation` class. – Alexey Mezenin Jan 08 '18 at 21:43
  • Hi @alexey-mezenin I'm pretty sure this is a bug. I've written notifications many times, it looks normal. – John Halsey Jan 08 '18 at 22:32
0

You must set MAIL_NAME in .env

In 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

Joan Nguyen
  • 372
  • 1
  • 5
0

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.

John Halsey
  • 1,930
  • 3
  • 19
  • 41
0

You have to put name inside 'to' array In config\mail.php

to' => [
            'address' => env('MAIL_TO_ADDRESS', 'hello@example.com'),
            'name' => env('MAIL_FROM_NAME', 'Example'),
     ],

and in respective model

public function routeNotificationForMail($notification)
          {
                return $this->email_address=config('mail.to.address');
           
          }

see laravel official doc laravel@8.x