spatie/laravel-mailcoach-sendgrid-setup
Helps Mailcoach prepare Sendgrid accounts to handle campaign feedback events (bounces, complaints, etc.) for emails sent via Sendgrid. Intended for internal use by Mailcoach; code is usable but has no standalone docs or support.
Installation
composer require spatie/laravel-mailcoach-sendgrid-setup
Ensure spatie/laravel-mailcoach is also installed (this package is a dependency for SendGrid integration).
Publish Configuration
php artisan vendor:publish --provider="Spatie\MailcoachSendgridSetup\MailcoachSendgridSetupServiceProvider"
This publishes the mailcoach-sendgrid-setup.php config file to config/.
Configuration
Add your SendGrid API key to .env:
SENDGRID_API_KEY=your_sendgrid_api_key_here
Configure the required SendGrid settings in config/mailcoach-sendgrid-setup.php:
'api_key' => env('SENDGRID_API_KEY'),
'from_email' => 'noreply@yourdomain.com',
'from_name' => 'Your Company',
Run Setup Command Execute the setup command to configure SendGrid for Mailcoach:
php artisan mailcoach:sendgrid:setup
This creates necessary SendGrid templates, categories, and API configurations.
config/mail.php:
'default' => env('MAIL_MAILER', 'mailcoach'),
'mailers' => [
'mailcoach' => [
'transport' => 'sendgrid',
],
],
Pre-Setup
mail.send (for sending emails).suppression (for managing bounces/unsubscribes).templates (for dynamic template management).Dynamic Template Management The package automates the creation of SendGrid dynamic templates for Mailcoach campaigns.
mailcoach_campaign_123).mailcoach:sendgrid:create-template command to generate templates manually if needed:
php artisan mailcoach:sendgrid:create-template --campaign-id=123
{{ name }}) to SendGrid’s template variables in the config:
'template_variables' => [
'name' => 'Name',
'email' => 'Email',
],
Feedback Loop Configuration
https://your-app.com/mailcoach/sendgrid/webhook
Route::post('/mailcoach/sendgrid/webhook', [\Spatie\Mailcoach\Mailcoach::class, 'handleSendgridWebhook']);
Batch Sending
config/mailcoach-sendgrid-setup.php:
'batch_size' => 100, // Emails per batch
'delay_between_batches' => 60, // Seconds
Testing
SENDGRID_SANDBOX_MODE=true
yourusername@yourdomain.com.sandboxXXXX.sendgrid.net).php artisan queue:work
config/mailcoach-sendgrid-setup.php:
'debug' => env('MAILCOACH_SENDGRID_DEBUG', false),
config/mail.php for when SendGrid fails:
'fallback' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.example.com'),
],
use Spatie\MailcoachSendgridSetup\Webhook\VerifySendgridWebhookSignature;
Route::post('/mailcoach/sendgrid/webhook', function (Request $request) {
if (!VerifySendgridWebhookSignature::verify($request)) {
abort(403);
}
// Process webhook
});
API Key Permissions
suppression access) cause bounces/unsubscribes to fail silently.Template Variable Mismatches
template_variables in the config and ensure SendGrid template variables match exactly (case-sensitive).Webhook Failures
curl:
curl -X POST https://your-app.com/mailcoach/sendgrid/webhook \
-H "Content-Type: application/json" \
-H "X-SendGrid-Signature: YOUR_SIGNATURE" \
-d '{"event": "bounce", "email": "test@example.com"}'
403 Forbidden errors.Rate Limits
batch_size and delay_between_batches in the config or upgrade your SendGrid plan.Sandbox Mode Pitfalls
SENDGRID_SANDBOX_MODE=false
Enable Debug Mode
Add to .env:
MAILCOACH_SENDGRID_DEBUG=true
Check logs for detailed SendGrid API responses.
Inspect SendGrid Templates
curl -X GET https://api.sendgrid.com/v3/templates \
-H "Authorization: Bearer YOUR_API_KEY"
Test Webhook Locally Use ngrok to expose your local webhook endpoint:
ngrok http 8000
Configure SendGrid to point to https://your-ngrok-subdomain.ngrok.io/mailcoach/sendgrid/webhook.
Check Mailcoach Logs Run:
php artisan mailcoach:logs
Look for Sendgrid entries to debug sending issues.
Custom Template Naming
Override the default template naming convention by extending the Spatie\MailcoachSendgridSetup\TemplateCreator class:
namespace App\Services;
use Spatie\MailcoachSendgridSetup\TemplateCreator as BaseTemplateCreator;
class CustomTemplateCreator extends BaseTemplateCreator
{
protected function getTemplateName(int $campaignId): string
{
return "custom_prefix_{$campaignId}";
}
}
Bind it in a service provider:
$this->app->bind(
\Spatie\MailcoachSendgridSetup\TemplateCreator::class,
App\Services\CustomTemplateCreator::class
);
Additional Webhook Events Extend the webhook handler to process custom events (e.g., clicks):
use Spatie\MailcoachSendgridSetup\Webhook\HandleSendgridWebhook;
class CustomWebhookHandler extends HandleSendgridWebhook
{
protected function handleClickEvent(array $payload)
{
// Custom logic
How can I help you explore Laravel packages today?