laravelemailreminders

Laravel automated invoice reminder email on due date, after 3days, after a week from different users to different clients


I am implementing a invoice API system for that I have to implement an automated reminder email on due date, after 3days, after 7days (it is like a API so the email has to be send from login user email to the selected customer)

For this system so far I implemented a checkbox selection which will store the login user, selected customer, due date, day selection for reminder stored the data in database). I have issues in sending emails from different users, adding condition to send email from the due date

console->commands->SendReminderemail.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
Use App\Reminder;
use App\Donor;
use App\Mail\ReminderEmailDigest;
Use Mail;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Invoice Reminder Emails';

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

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

        $pending_Reminders = Reminder::where('status',0)->get();
        
        $data = [];

        foreach ($pending_Reminders as $reminder){
            $data[$reminder->donor_id][] = $reminder->toArray();        
        }

        foreach($data as $donor_id => $pending_Reminders){
            $this->sendEmailToUser($donor_id, $pending_Reminders);
        }
        // dd($data);
    }

    private function sendEmailToUser($donor_id, $pending_Reminders){

        $user = Donor::find($donor_id);

        Mail::to($user)->send(new ReminderEmailDigest($pending_Reminders));

        // dd($user);
    }

} 

App->mail->ReminderEmailDigest

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ReminderEmailDigest extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    private $pending_Reminders;

    public function __construct($pending_Reminders)
    {
        $this->reminders = $pending_Reminders;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.reminder-digest')
        ->with('pending_Reminders', $this->reminder);
    }
}

App->console->kernal

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\SendReminderEmails;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */


    protected $commands = [
        SendReminderEmails::class,
    ];
    
    
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('reminder:emails')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

    
}

Now only the email working when I enter the reminder:emails command in console

How can i automate the mail?? any related suggestions??


Solution

  • Just a note on how you could clean up your code.

        $pending_Reminders = Reminder::where('status',0)
            ->get()
            ->groupBy('donor_id')
            ->each(function($reminders, $donor_id) {
                $user = Donor::find($donor_id);
                Mail::to($user)->send(new ReminderEmailDigest($reminders->toArray()));
            });
    

    To set the artisan command to run daily,

    app/Console/Kernel.php

        protected function schedule(Schedule $schedule)
        {
            $schedule->command('reminder:emails')->daily();
        }
    

    To get that to run sudo crontab -e

    * * * * * cd path/to/project && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1