-
app/Mail/PendingNotifications.php
Open in GitHubuse App\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; final class PendingNotifications extends Mailable implements ShouldQueue { use Queueable, SerializesModels; public function __construct( public readonly User $user, public readonly int $pendingNotificationsCount, ) { // } public function envelope(): Envelope { return new Envelope( subject: '🌸 Pinkary: You Have '.$this->pendingNotificationsCount.' '.str('Notification')->plural($this->pendingNotificationsCount).'! - '.now()->format('F j, Y'), ); } public function content(): Content { return new Content( markdown: 'mail.pending-notifications', with: [ 'date' => now()->format('Y-m-d'), 'user' => $this->user, 'pendingNotificationsCount' => $this->pendingNotificationsCount, ], ); } public function attachments(): array { return []; } }
-
resources/views/mail/pending-notifications.blade.php
Open in GitHub<x-mail::message> # Hello, {{ $user->name }}! We've noticed you have {{ $pendingNotificationsCount }} {{ Str::plural('notification', $pendingNotificationsCount) }}. You can view notifications by clicking the button below. <x-mail::button :url="route('notifications.index')"> View Notifications </x-mail::button> If you no longer wish to receive these emails, you can change your "Mail Preference Time" in your [profile settings]({{ route('profile.edit') }}). See you soon,<br> {{ config('app.name') }} </x-mail::message>
-
app/Console/Commands/SendUnreadNotificationEmailsCommand.php
Open in GitHubuse App\Enums\UserMailPreference; use App\Mail\PendingNotifications; use App\Models\User; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Mail; final class SendUnreadNotificationEmailsCommand extends Command { // ... public function handle(): void { User::query() ->when($this->option('weekly'), function (Builder $query): void { $query->where('mail_preference_time', UserMailPreference::Weekly); }, function (Builder $query): void { $query->where('mail_preference_time', UserMailPreference::Daily); }) ->whereHas('notifications') ->withCount('notifications') ->each(fn (User $user) => Mail::to($user)->queue(new PendingNotifications($user, $user->notifications_count))); } }