admin管理员组

文章数量:1122832

On a Laravel site I use jobs to send email notification like in app/Jobs/RecreateUserPasswordJob.php:

<?php

namespace App\Jobs;

use App\Models\User;
use App\Notifications\RecreateUserPasswordNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;
use phpDocumentor\Reflection\Types\Boolean;

class RecreateUserPasswordJob implements ShouldQueue
{
    use Queueable;

    protected User $user;
    protected string $password;

    public function __construct(User $user, string $password)
    {
        $this->user = $user;
        $this->password = $password;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        Mail::to($this->user)->send(new RecreateUserPasswordNotification($this->password));
    }
}

and in app/Notifications/RecreateUserPasswordNotification :

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class RecreateUserPasswordNotification extends Mailable
//class RecreateUserPasswordNotification extends Notification
{
    use Queueable;

    protected string $password;

    /**
     * Create a new notification instance.
     */
    public function __construct(string $password)
    {
        $this->password = $password;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->line('New password for you was generated : '.$this->password)
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}

and got error :

[2024-11-21 14:46:28] local.ERROR: Invalid view. {"exception":"[object] (InvalidArgumentException(code: 0): Invalid view. at /ProjectPath/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:404)
[stacktrace]
#0 /ProjectPath/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(309): Illuminate\\Mail\\Mailer->parseView()
#1 /ProjectPath/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(205): Illuminate\\Mail\\Mailer->send()
#2 /ProjectPath/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Illuminate\\Mail\\Mailable->Illuminate\\Mail\\{closure}()
#3 /ProjectPath/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(198): Illuminate\\Mail\\Mailable->withLocale()
#4 /ProjectPath/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(354): Illuminate\\Mail\\Mailable->send()
#5 /ProjectPath/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(301): Illuminate\\Mail\\Mailer->sendMailable()
#6 /ProjectPath/vendor/laravel/framework/src/Illuminate/Mail/PendingMail.php(124): Illuminate\\Mail\\Mailer->send()
#7 /ProjectPath/app/Jobs/RecreateUserPasswordJob.php(30): Illuminate\\Mail\\PendingMail->send()
#8 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\\Jobs\\RecreateUserPasswordJob->handle()
#9 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#10 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure()
#11 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#12 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call()
#13 /ProjectPath/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\\Container\\Container->call()
#14 /ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}()
#15 /ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#16 /ProjectPath/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\\Pipeline\\Pipeline->then()
#17 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(124): Illuminate\\Bus\\Dispatcher->dispatchNow()
#18 /ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Queue\\CallQueuedHandler->Illuminate\\Queue\\{closure}()
#19 /ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#20 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(123): Illuminate\\Pipeline\\Pipeline->then()
#21 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(71): Illuminate\\Queue\\CallQueuedHandler->dispatchThroughMiddleware()
#22 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(102): Illuminate\\Queue\\CallQueuedHandler->call()
#23 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(440): Illuminate\\Queue\\Jobs\\Job->fire()
#24 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(390): Illuminate\\Queue\\Worker->process()
#25 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(177): Illuminate\\Queue\\Worker->runJob()
#26 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(141): Illuminate\\Queue\\Worker->daemon()
#27 /ProjectPath/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(124): Illuminate\\Queue\\Console\\WorkCommand->runWorker()
#28 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Queue\\Console\\WorkCommand->handle()
#29 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#30 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure()
#31 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#32 /ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call()
#33 /ProjectPath/vendor/laravel/framework/src/Illuminate/Console/Command.php(213): Illuminate\\Container\\Container->call()
#34 /ProjectPath/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#35 /ProjectPath/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Symfony\\Component\\Console\\Command\\Command->run()
#36 /ProjectPath/vendor/symfony/console/Application.php(1047): Illuminate\\Console\\Command->run()
#37 /ProjectPath/vendor/symfony/console/Application.php(316): Symfony\\Component\\Console\\Application->doRunCommand()
#38 /ProjectPath/vendor/symfony/console/Application.php(167): Symfony\\Component\\Console\\Application->doRun()
#39 /ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(197): Symfony\\Component\\Console\\Application->run()
#40 /ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1203): Illuminate\\Foundation\\Console\\Kernel->handle()
#41 /ProjectPath/artisan(13): Illuminate\\Foundation\\Application->handleCommand()
#42 {main}

How to fix this error ?

"laravel/framework": "^11.27.2",

本文标签: laravelWhy using jobs to send email notification I got errorStack Overflow