Day 3: Sending AI Debugging Suggestions via Email and Slack Notifications
In Day 2, we captured Laravel error logs and processed them using an AI-powered Laravel debugger. Today, we’ll send AI suggestions via email and Slack for faster visibility and real-time collaboration.
Step 1: Configure Laravel Mail
Open .env
and add your mail driver settings (use Mailtrap for testing):
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=null
[email protected]
MAIL_FROM_NAME="AI Laravel Debugger"
Test it with:
php artisan config:clear
php artisan config:cache
Step 2: Create a Mail Notification Class
Run:
php artisan make:mail AIDebuggerSuggestionMail
Open app/Mail/AIDebuggerSuggestionMail.php
and update:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class AIDebuggerSuggestionMail extends Mailable
{
use Queueable, SerializesModels;
public $errorMessage;
public $suggestion;
public function __construct($errorMessage, $suggestion)
{
$this->errorMessage = $errorMessage;
$this->suggestion = $suggestion;
}
public function build()
{
return $this->subject('AI Debugger Suggestion')
->view('emails.ai_suggestion');
}
}
Create the email view:
mkdir -p resources/views/emails
touch resources/views/emails/ai_suggestion.blade.php
And add:
<h2>AI-Powered Laravel Debugger Suggestion</h2>
<p><strong>Error:</strong> {{ $errorMessage }}</p>
<p><strong>AI Suggestion:</strong> {{ $suggestion }}</p>
Step 3: Send Email from Listener
Modify your LogErrorListener.php
:
use App\Mail\AIDebuggerSuggestionMail;
use Illuminate\Support\Facades\Mail;
public function handle(MessageLogged $event)
{
if ($event->level === 'error') {
$suggestion = $this->aiDebugger->analyzeError($event->message);
// Log to file
\Log::channel('ai_debugger')->error('AI Suggestion: ' . $suggestion);
// Send email
Mail::to('[email protected]')->send(
new AIDebuggerSuggestionMail($event->message, $suggestion)
);
}
}
Step 4: Configure Slack Notifications (Optional)
Install Slack notification channel:
composer require laravel/slack-notification-channel
In .env
, add:
LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/webhook/url
Then, in config/logging.php
, add a new channel:
'slack_ai' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel AI Debugger',
'emoji' => ':robot_face:',
'level' => 'error',
],
Update the LogErrorListener.php
to also send to Slack:
\Log::channel('slack_ai')->error("AI Debugger Suggestion\nError: {$event->message}\nSuggestion: $suggestion");
Step 5: Test Everything
Visit:
http://127.0.0.1:8000/test-error
You should now receive:
- An email with the error and suggestion.
- A Slack message if configured.
Next Steps
- In Day 4, we’ll build a dashboard to view AI error suggestions historically inside Laravel.
No more digging through logs—let AI and notifications bring debugging to you.