dugandzic/swiftmailer-mailgun-bundle
Installation
composer require dugandzic/swiftmailer-mailgun-bundle
(Note: The README references cspoo/swiftmailer-mailgun-bundle, but the package name in the prompt is dugandzic/swiftmailer-mailgun-bundle. Use the correct namespace in your AppKernel.php.)
Register the Bundle
In app/AppKernel.php:
new \Dugandzic\Swiftmailer\MailgunBundle\DugandzicSwiftmailerMailgunBundle(),
Configure Mailgun
In config/packages/dugandzic_swiftmailer_mailgun.yaml (or config/config.yml for older Laravel):
dugandzic_swiftmailer_mailgun:
key: "%env(MAILGUN_API_KEY)%" # Use .env for security
domain: "%env(MAILGUN_DOMAIN)%"
Update Swiftmailer Transport In the same config file:
framework:
mailer:
transport: "%env(MAILER_TRANSPORT)%" # Set to "mailgun"
First Use Case
Send a test email via Laravel’s Mail facade:
use Illuminate\Support\Facades\Mail;
Mail::raw('Test email body', function ($message) {
$message->to('recipient@example.com')
->subject('Test Subject');
});
Mailgun-Specific Features
dugandzic_swiftmailer_mailgun:
track_opens: true
track_clicks: true
$message->attach()). Mailgun handles file uploads automatically.Environment-Based Configuration
Use .env for dynamic keys/domains:
MAILGUN_API_KEY=key-xxxxxxxxxx
MAILGUN_DOMAIN=mydomain.com
MAILER_TRANSPORT=mailgun
Queueing Emails Pair with Laravel’s queue system for async sending:
Mail::to('user@example.com')->queue(new OrderShipped($order));
Custom Headers
Inject Mailgun-specific headers (e.g., X-Mailgun-Variables):
$message->getHeaders()->addTextHeader('X-Mailgun-Variables', '{"user_id": 123}');
Mailable to use Mailgun’s features:
public function build()
{
return $this->markdown('emails.order')
->with(['tracking' => true]);
}
public function handle(IncomingMailgunWebhook $event)
{
// Process event (e.g., "clicked", "delivered")
}
API Key Security
config.yml. Always use .env or Laravel’s env() helper.sending scope).Domain Mismatch
domain in config matches the Mailgun domain you’re using. Errors like Invalid domain often stem from this.Rate Limits
Spoofing Protection
Enable Logging
Add to config/logging.php:
'channels' => [
'mailgun' => [
'driver' => 'single',
'path' => storage_path('logs/mailgun.log'),
'level' => 'debug',
],
],
Then log Mailgun responses:
\Log::channel('mailgun')->debug('Mailgun response:', $response);
Test Mode
Use Mailgun’s sandbox domain (*.mailgun.org) for testing to avoid real sends.
Custom Transports
Extend the bundle’s MailgunTransport to add features:
class CustomMailgunTransport extends \Dugandzic\Swiftmailer\MailgunBundle\MailgunTransport
{
public function __construct($apiKey, $domain, $customOption)
{
parent::__construct($apiKey, $domain);
$this->customOption = $customOption;
}
}
Event Listeners
Subscribe to Swiftmailer events (e.g., SentEvent) to log or modify emails:
$mailer->getTransport()->getEventDispatcher()->addListener(
'sent',
function ($event) {
// Custom logic (e.g., analytics)
}
);
Override Templates Use Mailgun’s template variables in Laravel:
$message->setFrom('noreply@mydomain.com')
->setTemplateId('welcome_template');
How can I help you explore Laravel packages today?