azine/mailgunwebhooks-bundle
Install the Bundle
Add to composer.json:
"azine/mailgunwebhooks-bundle": "dev-master"
Run composer update.
Register the Bundle
Add to config/bundles.php:
return [
// ...
Azine\MailgunWebhooksBundle\AzineMailgunWebhooksBundle::class => ['all' => true],
];
Configure Mailgun Webhook Endpoint Run:
php bin/console debug:router | grep mailgunevent_webhook
Copy the generated URL (e.g., /mailgun-webhook) and add it to your Mailgun Webhooks settings under Settings > Webhooks.
Configure Bundle
Add to config/packages/azine_mailgun_webhooks.yaml:
azine_mailgun_webhooks:
api_key: "%env(MAILGUN_API_KEY)%"
email_domain: "yourdomain.com"
no_reply_email: "no-reply@yourdomain.com"
no_reply_name: "Your App Name"
Test the Webhook
Send a test email via Mailgun and verify the event appears in the admin panel at /admin/mailgun.
Webhook Endpoint
Mailgun POSTs event data to /mailgun-webhook. The bundle automatically:
api_key).MailgunEvent entity (Doctrine ORM).MailgunWebhookEvent for custom logic (e.g., notifications).Admin Interface
/admin/mailgun (Twig templates).Event-Driven Extensions
Subscribe to MailgunWebhookEvent in a service:
// src/EventListener/CustomMailgunListener.php
namespace App\EventListener;
use Azine\MailgunWebhooksBundle\Event\MailgunWebhookEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomMailgunListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
MailgunWebhookEvent::NAME => 'onMailgunEvent',
];
}
public function onMailgunEvent(MailgunWebhookEvent $event)
{
$eventData = $event->getEvent();
if ($eventData->getEvent() === 'failed') {
// Trigger custom logic (e.g., Slack alert)
}
}
}
CLI Automation
php bin/console mailgun:delete-events --date="30 days ago"
php bin/console mailgun:check-ip-in-blacklist --numberOfAttempts=3
Configure hetrixtools_service in config/packages/azine_mailgun_webhooks.yaml for API access.SwiftMailer Integration
Use SwiftMailerMailgunWebhookEventListener (if available in future updates) to auto-notify admins of critical events (e.g., bounces).
Database Optimization
MailgunEvent for frequently queried fields (e.g., event, timestamp).mailgun:delete-events via cron (e.g., weekly):
0 3 * * 1 php /path/to/bin/console mailgun:delete-events --date="90 days ago"
Monolog Filtering Exclude non-critical 404s from Mailgun logs to avoid clutter:
monolog:
handlers:
main:
type: fingers_crossed
excluded_404s:
- ".*/api/.*" # Example: Ignore API 404s
Spam Alerts Enable spam notifications in config:
spam_alerts:
enabled: true
alerts_recipient_email: "admin@example.com"
Mailgun will email admins if your sending IP is blacklisted (via HetrixTools).
Webhook Signature Validation
api_key in config matches your Mailgun API key./mailgun-webhook with a valid signature:
curl -X POST http://yourdomain.test/mailgun-webhook \
-H "Content-Type: application/json" \
-H "X-Mailgun-Signature: YOUR_SIGNATURE" \
-d '{"event":"sent", "timestamp":123456789}'
Database Bloat
mailgun:delete-events to avoid storage issues.HetrixTools API Limits
repeat_notification_after_days to avoid redundant checks:
hetrixtools_service:
repeat_notification_after_days: 7
Event Overload
// src/MessageHandler/MailgunEventHandler.php
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
public function __invoke(MailgunEvent $event)
{
// Process event (e.g., save to DB)
}
Symfony 2.x to 4+/5+/6+ Migration
AppKernel → bundles.php).Webhook Failures
var/log/dev.log) for validation errors.X-Mailgun-Signature header matches Mailgun’s calculation:
# Test signature locally
echo -n "timestamp=123456789" | openssl dgst -sha1 -hmac "YOUR_API_KEY" -binary | openssl enc -base64 -A
CLI Command Issues
mailgun:check-ip-in-blacklist, ensure hetrixtools_service.api_key is set and the IP is resolvable.php bin/console debug:container Azine\MailgunWebhooksBundle\Command\CheckIpInBlacklistCommand
Admin UI Glitches
php bin/console cache:clear
var/log/dev.log.Custom Event Handlers
Extend the MailgunWebhookEvent subscriber pattern to add logic for specific events (e.g., "clicked" → update user analytics):
// src/EventListener/AnalyticsListener.php
public function onMailgunEvent(MailgunWebhookEvent $event)
{
if ($event->getEvent()->getEvent() === 'clicked') {
$this->analyticsService->track($event->getEvent()->getRecipient());
}
}
Custom Blacklist Providers
Replace HetrixTools with another service (e.g., Spamhaus) by implementing Azine\MailgunWebhooksBundle\Service\BlacklistCheckerInterface.
Webhook Retries Add retry logic for failed webhook deliveries using Symfony Messenger or a queue system.
Event Enrichment
Join MailgunEvent with other entities (e.g., users) in the admin list view:
{# templates/mailgun/event/list.html.twig #}
<td>{{ event.recipient|split('@')[0] }} {# Extract email local part #}</td>
How can I help you explore Laravel packages today?