Day 6 – Automating Weekly Audit Reports with GPT and Email #LaravelGPTAudit #AuditAutomation #LaravelScheduler #AIAuditReports


Today we’ll build a scheduled task that runs every week (or month), gathers recent audit logs, summarizes them with GPT, and emails the results to your security or management team — fully automated.


📦 Step 1: Create a custom Artisan command

php artisan make:command SendWeeklyAuditReport

In app/Console/Commands/SendWeeklyAuditReport.php:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Spatie\Activitylog\Models\Activity;
use App\Helpers\AuditSummaryHelper;

class SendWeeklyAuditReport extends Command
{
    protected $signature = 'audit:send-weekly-report';
    protected $description = 'Send GPT-generated audit summary every week';

    public function handle(): int
    {
        $logs = Activity::with('causer.roles', 'causer.department')
            ->where('created_at', '>=', now()->subWeek())
            ->latest()->get();

        if ($logs->isEmpty()) {
            $this->info('No logs to report.');
            return 0;
        }

        $summary = AuditSummaryHelper::summarizeByRole($logs);

        Mail::to(['[email protected]', '[email protected]'])->send(
            new \App\Mail\WeeklyAuditSummaryMail($summary)
        );

        $this->info('Audit summary sent!');
        return 0;
    }
}

📨 Step 2: Create the mailable

php artisan make:mail WeeklyAuditSummaryMail

In app/Mail/WeeklyAuditSummaryMail.php:

namespace App\Mail;

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

class WeeklyAuditSummaryMail extends Mailable
{
    use Queueable, SerializesModels;

    public string $summary;

    public function __construct(string $summary)
    {
        $this->summary = $summary;
    }

    public function build(): self
    {
        return $this->subject('Weekly Laravel Audit Report')
            ->view('emails.audit_summary')
            ->with(['summary' => $this->summary]);
    }
}

📧 Step 3: Create the email view

In resources/views/emails/audit_summary.blade.php:

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Audit Summary</title></head>
<body>
    <h2>GPT-Powered Weekly Audit Summary</h2>
    <pre style="font-family: monospace; white-space: pre-wrap;">
{{ $summary }}
    </pre>
</body>
</html>

⏰ Step 4: Schedule the command

In App\Console\Kernel.php:

protected function schedule(Schedule $schedule): void
{
    $schedule->command('audit:send-weekly-report')->weeklyOn(1, '08:00');
}

This will run every Monday at 8 AM.


🧪 Step 5: Test it manually

php artisan audit:send-weekly-report

You should receive an email with grouped GPT summaries of the last 7 days’ logs.


✅ Tomorrow (Day 7), we’ll enhance log readability by translating technical entries into plain business English — no developer jargon needed.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.