簡體   English   中英

如何在 laravel 中發送帶有調度隊列的 email

[英]How to send email with schedule queue in laravel

我想在 laravel 項目上使用工匠命令發送 email

所以我創建了命令並嘗試使用工匠命令發送 email 。

<?php
    
    namespace App\Console;
    
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        /**
         * The Artisan commands provided by your application.
         *
         * @var array
         */
        protected $commands = [
            Commands\DailyQuote::class,
        ];
    
        /**
         * Define the application's command schedule.
         *
         * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
         * @return void
         */
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('quote:daily')
                ->everyMinute();;
        }
    
        /**
         * Register the commands for the application.
         *
         * @return void
         */
        protected function commands()
        {
            $this->load(__DIR__.'/Commands');
    
            require base_path('routes/console.php');
        }
    }

我已經像上面一樣制作了 app/console/kernal.php。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Mail\InvoiceNotification;

use Mail;

class DailyQuote extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'quote:daily';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send notification to user who will expire';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $details = [
            'lastname' => '$client->lastname',
            'invoiceUrl' => 'https://billing.hutv.me/invoice_view/invoice_id',
            'invoiceDueDate' => '$client->expiration'
        ];
        Mail::to('receiver@mail.com')->send(new InvoiceNotification($details));
        
        $this->info('Successfully sent daily quote to everyone.');
    }
}

這是我的app/console/commands/DailyQuote.php

我已經在 Ubuntu 服務器上使用php artisan quote:daily命令執行

但是 Email 沒有發送沒有錯誤。

所以我試圖在我的其他 controller 上調用命令

Artisan::call('quote:daily');

它正在工作並發送 email。

我不知道這個問題有什么問題。

如果有人有好的解決方案,請幫助我。

謝謝。

通過php artisan make:notification InvoiceNotification ,這應該實現 ShouldQueue 如下:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Mail\BaseMail; // optionaly your email template

class InvoiceNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function toMail($notifiable)
    {
        return (new BaseMail())
                    ->subject('your subject')
                    ->to($notifiable->email);
    }
    // ...
}

然后發送例如


    public function handle()
    {

        $details = [
            'lastname' => '$client->lastname',
            'invoiceUrl' => 'https://billing.hutv.me/invoice_view/invoice_id',
            'invoiceDueDate' => '$client->expiration'
        ];
 
        Notification::route('mail', 'receiver@mail.com')
                                    ->notify(new InvoiceNotification($details))
        $this->info('Successfully sent daily quote to everyone.');
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM