spatie/laravel-mailcoach-sendgrid-feedback
Add-on for spatie/laravel-mailcoach that processes Sendgrid feedback for your email campaigns, handling events like bounces, complaints, and other delivery signals so Mailcoach can track outcomes and keep lists clean.
Installation:
composer require spatie/laravel-mailcoach-sendgrid-feedback
Publish the config file:
php artisan vendor:publish --provider="Spatie\MailcoachSendgridFeedback\MailcoachSendgridFeedbackServiceProvider"
Configuration:
spatie/laravel-mailcoach is installed and configured..env:
SENDGRID_API_KEY=your_api_key_here
config/mailcoach-sendgrid-feedback.php:
'webhook_secret' => env('MAILCOACH_SENDGRID_FEEDBACK_WEBHOOK_SECRET'),
'webhook_url' => env('MAILCOACH_SENDGRID_FEEDBACK_WEBHOOK_URL'),
First Use Case:
routes/web.php:
Route::post('/mailcoach/sendgrid-feedback', [\Spatie\MailcoachSendgridFeedback\Http\Controllers\SendgridFeedbackController::class, 'handle']);
Webhook Handling:
bounce, click, open) to your Laravel app via HTTP POST.webhook_secret and processes the payload.Feedback Processing:
Spatie\MailcoachSendgridFeedback\FeedbackProcessors\FeedbackProcessor class to customize how feedback is handled:
namespace App\MailcoachSendgridFeedback;
use Spatie\MailcoachSendgridFeedback\FeedbackProcessors\FeedbackProcessor;
class CustomFeedbackProcessor extends FeedbackProcessor
{
public function processBounce($bounce)
{
// Custom logic for bounces (e.g., log to a custom table)
}
public function processClick($click)
{
// Custom logic for clicks (e.g., track in analytics)
}
}
AppServiceProvider:
public function boot()
{
$this->app->bind(
\Spatie\MailcoachSendgridFeedback\FeedbackProcessors\FeedbackProcessor::class,
\App\MailcoachSendgridFeedback\CustomFeedbackProcessor::class
);
}
Campaign Tracking:
use Spatie\Mailcoach\Models\Feedback;
$bounces = Feedback::where('type', 'bounce')->get();
Event Listeners:
use Spatie\MailcoachSendgridFeedback\Events\FeedbackProcessed;
FeedbackProcessed::listen(function ($feedback) {
// Example: Send Slack notification for hard bounces
if ($feedback->type === 'bounce' && $feedback->severity === 'hard') {
// Notify team
}
});
Webhook Secret Mismatch:
webhook_secret in .env matches the one configured in SendGrid’s webhook settings.403 Forbidden errors if the secret is incorrect.Payload Validation:
\Log::debug('Raw SendGrid feedback payload:', ['payload' => $request->getContent()]);
Rate Limiting:
// In CustomFeedbackProcessor
public function processFeedback($feedback)
{
\Queue::push(new ProcessFeedbackJob($feedback));
}
Time Zone Sync:
$feedback->created_at = now();
Testing Webhooks Locally:
Custom Feedback Types:
Feedback model to add custom fields:
use Spatie\Mailcoach\Models\Feedback as BaseFeedback;
class Feedback extends BaseFeedback
{
protected $casts = [
'custom_field' => 'string',
];
}
Monitoring:
Security:
VerifyCsrfToken middleware for additional protection (though the package handles CSRF via the secret).Performance:
// In CustomFeedbackProcessor
public function processFeedback($feedback)
{
if (collect($this->buffer)->count() >= 100) {
$this->flushBuffer();
}
$this->buffer[] = $feedback;
}
How can I help you explore Laravel packages today?