renzojohnson / mailgun-api
Lightweight PHP wrapper for Mailgun API. Send emails, validate addresses, manage domains. Zero dependencies beyond ext-curl.
v0.26.03.16
2026-03-17 02:15 UTC
Requires
- php: ^8.4
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^11.0 || ^12.0
This package is auto-updated.
Last update: 2026-03-17 02:17:49 UTC
README
Lightweight PHP wrapper for Mailgun API. Send emails, validate addresses, manage domains. Zero dependencies beyond ext-curl.
Author: Renzo Johnson
Requirements
- PHP 8.4 or higher
ext-curlext-json
Installation
composer require renzojohnson/mailgun-api
Quick Start
use RenzoJohnson\Mailgun\Mailgun; $mailgun = new Mailgun('key-your-api-key', 'mg.yourdomain.com'); // Send a simple text email $mailgun->sendText( 'sender@mg.yourdomain.com', 'recipient@example.com', 'Hello from Mailgun', 'This is the email body.' );
Send HTML Email
$mailgun->sendHtml( 'sender@mg.yourdomain.com', 'recipient@example.com', 'Welcome!', '<h1>Welcome</h1><p>Thanks for signing up.</p>', 'Welcome! Thanks for signing up.' // optional plain text fallback );
Advanced Email Builder
use RenzoJohnson\Mailgun\Message\Email; $email = new Email( 'sender@mg.yourdomain.com', 'recipient@example.com', 'Order Confirmation', text: 'Your order has been confirmed.', html: '<h1>Order Confirmed</h1>' ); $email->cc('manager@example.com') ->bcc('archive@example.com') ->replyTo('support@example.com') ->header('X-Campaign', 'welcome-series') ->variable('order_id', '12345') ->tag('transactional'); $mailgun->send($email);
Domain Info
// Get domain details $info = $mailgun->getDomainInfo(); // List all domains $domains = $mailgun->listDomains();
Events & Stats
// Get sending events $events = $mailgun->getEvents(['event' => 'delivered', 'limit' => 10]); // Get delivery stats (last 7 days) $stats = $mailgun->getStats('delivered', 7);
Email Validation
// Validate an email address (requires validation API key) $result = $mailgun->validateEmail('user@example.com');
Error Handling
use RenzoJohnson\Mailgun\Exception\ApiException; try { $mailgun->sendText('from@mg.example.com', 'to@example.com', 'Test', 'Body'); } catch (ApiException $e) { echo $e->getMessage(); // "Mailgun API error (401): Forbidden" }
Links
License
MIT License. Copyright (c) 2026 Renzo Johnson.