Top Related Projects
Helps sending emails
The classic email sending library for PHP
Comprehensive mailing tools for PHP
The Official Twilio SendGrid PHP API Library
Quick Overview
Mailgun-php is the official PHP SDK for the Mailgun email automation service. It provides a simple and efficient way to integrate Mailgun's email sending, receiving, and tracking capabilities into PHP applications. This library simplifies the process of interacting with Mailgun's API, allowing developers to easily manage email campaigns and transactional emails.
Pros
- Easy integration with Mailgun's API
- Comprehensive support for Mailgun features (sending, receiving, tracking, etc.)
- Well-documented and actively maintained
- PSR-7 compliant, allowing for easy integration with modern PHP frameworks
Cons
- Requires a Mailgun account and API key
- Limited to Mailgun's service (not a generic email library)
- Learning curve for developers unfamiliar with Mailgun's concepts
- Dependency on external service may impact application reliability
Code Examples
- Sending a simple email:
use Mailgun\Mailgun;
$mg = Mailgun::create('your-api-key');
$mg->messages()->send('your-domain.com', [
'from' => 'sender@example.com',
'to' => 'recipient@example.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomeness!'
]);
- Adding an attachment to an email:
use Mailgun\Mailgun;
$mg = Mailgun::create('your-api-key');
$mg->messages()->send('your-domain.com', [
'from' => 'sender@example.com',
'to' => 'recipient@example.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomeness!',
'attachment' => [
['filePath' => '/path/to/file.jpg', 'filename' => 'photo.jpg']
]
]);
- Retrieving events:
use Mailgun\Mailgun;
$mg = Mailgun::create('your-api-key');
$events = $mg->events()->get('your-domain.com', [
'limit' => 25,
'filter' => 'delivered'
]);
Getting Started
-
Install the library using Composer:
composer require mailgun/mailgun-php php-http/guzzle6-adapter -
Create a Mailgun instance with your API key:
use Mailgun\Mailgun; $mg = Mailgun::create('your-api-key'); -
Use the instance to interact with Mailgun's API, for example, to send an email:
$mg->messages()->send('your-domain.com', [ 'from' => 'sender@example.com', 'to' => 'recipient@example.com', 'subject' => 'Hello', 'text' => 'It's so simple to send a message!' ]);
Competitor Comparisons
Helps sending emails
Pros of symfony/mailer
- Part of the Symfony ecosystem, integrating seamlessly with other Symfony components
- Supports multiple email providers and can easily switch between them
- Offers a more abstracted and flexible API for sending emails
Cons of symfony/mailer
- Steeper learning curve for developers not familiar with Symfony
- May have more overhead for simple email sending tasks
- Less specialized for Mailgun-specific features compared to mailgun-php
Code Comparison
mailgun-php:
$mg = Mailgun::create('key-example');
$mg->messages()->send('example.com', [
'from' => 'bob@example.com',
'to' => 'alice@example.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomeness!'
]);
symfony/mailer:
$email = (new Email())
->from('bob@example.com')
->to('alice@example.com')
->subject('Hello')
->text('Testing some Symfony Mailer awesomeness!');
$mailer->send($email);
Both libraries provide straightforward ways to send emails, but symfony/mailer offers a more object-oriented approach with its Email class. mailgun-php is more focused on Mailgun-specific features, while symfony/mailer provides a more generic interface that can work with various email providers.
The classic email sending library for PHP
Pros of PHPMailer
- Standalone library with no external dependencies
- Supports a wide range of email protocols (SMTP, mail(), sendmail)
- Extensive documentation and large community support
Cons of PHPMailer
- Requires more configuration and setup compared to Mailgun
- Limited built-in features for advanced email tracking and analytics
- May require additional server configuration for optimal performance
Code Comparison
PHPMailer:
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
Mailgun:
$mg = Mailgun::create('your-api-key');
$mg->messages()->send('example.com', [
'from' => 'Excited User <mailgun@example.com>',
'to' => 'recipient@example.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomeness!'
]);
The code comparison shows that Mailgun requires less setup and configuration, while PHPMailer offers more granular control over email settings. PHPMailer's code is more verbose but provides direct access to SMTP settings, while Mailgun abstracts these details away, resulting in simpler code for basic email sending tasks.
Comprehensive mailing tools for PHP
Pros of SwiftMailer
- Platform-independent and does not require external dependencies
- Supports a wide range of transport methods (SMTP, sendmail, etc.)
- Extensive documentation and community support
Cons of SwiftMailer
- No built-in support for advanced features like email tracking or analytics
- Requires more setup and configuration compared to Mailgun-PHP
- Less optimized for high-volume email sending
Code Comparison
SwiftMailer:
$transport = new Swift_SmtpTransport('smtp.example.org', 25);
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Subject'))
->setFrom(['john@doe.com' => 'John Doe'])
->setTo(['receiver@domain.org'])
->setBody('Here is the message itself');
$result = $mailer->send($message);
Mailgun-PHP:
$mg = Mailgun::create('your-api-key');
$mg->messages()->send('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'
]);
SwiftMailer offers more flexibility in transport methods and is platform-independent, making it suitable for various environments. However, Mailgun-PHP provides a simpler API for sending emails and includes built-in support for Mailgun-specific features. SwiftMailer requires more setup but offers greater control over the email sending process, while Mailgun-PHP is more straightforward to use but is tied to the Mailgun service.
The Official Twilio SendGrid PHP API Library
Pros of sendgrid-php
- More comprehensive documentation and examples
- Supports a wider range of email-related features (e.g., marketing campaigns, email validation)
- Active community and frequent updates
Cons of sendgrid-php
- Steeper learning curve due to more complex API
- Larger library size, which may impact performance in some cases
Code Comparison
sendgrid-php:
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
mailgun-php:
$mg = Mailgun::create('key-example');
$mg->messages()->send('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'
]);
Both libraries offer straightforward ways to send emails, but sendgrid-php uses a more object-oriented approach, while mailgun-php opts for a simpler function-based method. SendGrid's library provides more granular control over email components, which can be beneficial for complex use cases but may require more setup for basic tasks.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Mailgun PHP SDK
The official Mailgun PHP SDK â a clean, PSR-18 HTTP client wrapper around the Mailgun API.
Table of Contents
- Requirements
- Installation
- Quick Start
- Sending Email
- IP Management
- Dynamic IP Pools
- Analytics
- Subaccounts
- Response Handling
- Debugging
- Framework Integration
- Contributing
Requirements
- PHP 7.4 or higher
- A PSR-18 HTTP client (e.g.
symfony/http-client,guzzlehttp/guzzle) - A PSR-7 / PSR-17 implementation (e.g.
nyholm/psr7)
The SDK is not coupled to any specific HTTP library â bring your own PSR-18 client.
Installation
composer require mailgun/mailgun-php symfony/http-client nyholm/psr7
EU region? Use
https://api.eu.mailgun.netas your endpoint (see below).
Quick Start
require 'vendor/autoload.php';
use Mailgun\Mailgun;
// US servers (default)
$mg = Mailgun::create('your-api-key');
// EU servers
$mg = Mailgun::create('your-api-key', 'https://api.eu.mailgun.net');
Note: The
$domainyou pass to any API call must match a domain configured in app.mailgun.com.
Sending Email
Simple message
$mg->messages()->send('example.com', [
'from' => 'Alice <alice@example.com>',
'to' => 'bob@example.com',
'subject' => 'Hello from Mailgun!',
'text' => 'This is a plain-text body.',
'html' => '<p>This is an <strong>HTML</strong> body.</p>',
]);
With attachments and tracking options
$mg->messages()->send('example.com', [
'from' => 'alice@example.com',
'to' => ['bob@example.com', 'carol@example.com'],
'subject' => 'Monthly report',
'text' => 'Please find the report attached.',
'attachment' => [['filePath' => '/tmp/report.pdf', 'filename' => 'report.pdf']],
'o:tracking' => 'yes',
'o:tag' => ['monthly', 'report'],
]);
Scheduled delivery
$mg->messages()->send('example.com', [
'from' => 'alice@example.com',
'to' => 'bob@example.com',
'subject' => 'See you tomorrow',
'text' => 'Scheduled for tomorrow morning.',
'o:deliverytime' => 'tomorrow 9am UTC',
]);
IP Management
List all account IPs
$response = $mg->ips()->index();
foreach ($response->getItems() as $ip) {
echo $ip . PHP_EOL;
}
// Dedicated only
$dedicated = $mg->ips()->index(dedicated: true);
// With full details (pool assignments, subaccount ownership, timestamps)
$detailed = $mg->ips()->listIpsDetailed([
'pool_id' => 'my-pool-id', // filter by pool ('any' or 'none' also accepted)
'subaccount_id'=> 'sub-123',
'limit' => 25,
]);
foreach ($detailed->getItems() as $ip) {
echo "{$ip['address']} â pools: " . implode(', ', $ip['pool_ids']) . PHP_EOL;
}
Inspect a single IP
$ip = $mg->ips()->show('1.2.3.4');
echo $ip->getIp(); // "1.2.3.4"
echo $ip->getRdns(); // reverse DNS
var_dump($ip->getDedicated()); // bool
Assign / remove an IP on a specific domain
// Add IP to a domain
$mg->ips()->assign('example.com', '1.2.3.4');
// Remove IP from a domain
$mg->ips()->unassign('example.com', '1.2.3.4');
// List IPs currently assigned to a domain
$response = $mg->ips()->domainIndex('example.com');
print_r($response->getItems());
Bulk IP operations across all domains
// Assign an IP to every domain in the account (async)
$ref = $mg->ips()->assignIpToAllDomains('1.2.3.4');
echo $ref->getReferenceId(); // track the async operation
// Remove an IP from all domains, replacing it with another
$ref = $mg->ips()->removeIpFromAllDomains('1.2.3.4', alternative: '5.6.7.8');
echo $ref->getMessage();
Find all domains using a specific IP
$response = $mg->ips()->domainsByIp('1.2.3.4', limit: 20, search: 'example');
foreach ($response->getItems() as $domain) {
echo $domain . PHP_EOL;
}
Dedicated IP band
// Move an account IP into a dedicated IP band
$mg->ips()->placeAccountIpToBand('1.2.3.4');
Request a new dedicated IP
// Check how many dedicated IPs your plan allows
$available = $mg->ips()->numberOfIps();
// Provision a new dedicated IP
$mg->ips()->addDedicatedIp();
Dynamic IP Pools (DIPP)
Dynamic IP Pools let you group dedicated IPs and link them to domains, so sending traffic is spread across all IPs in the pool automatically.
List and inspect pools
// All pools in the account
$response = $mg->ips()->listIpPools();
foreach ($response->getIpPools() as $pool) {
echo "{$pool['pool_id']} â {$pool['name']}" . PHP_EOL;
echo " IPs: " . implode(', ', $pool['ips']) . PHP_EOL;
echo " Linked to domains: " . ($pool['is_linked'] ? 'yes' : 'no') . PHP_EOL;
}
// Single pool details
$pool = $mg->ips()->loadDIPPInformation('my-pool-id');
echo $pool->getPoolId(); // "my-pool-id"
echo $pool->getName(); // "Primary sending pool"
echo $pool->getDescription(); // "Main US sending pool"
print_r($pool->getIps()); // ["1.2.3.4", "5.6.7.8"]
var_dump($pool->isLinked()); // bool â whether domains are attached
Create and configure a pool
// Create a new pool
$mg->ips()->createIpPool('Primary Pool', 'Main US outbound pool');
// Modify pool metadata, add/remove IPs, link/unlink domains â all in one call
$mg->ips()->updateIpPool('my-pool-id', [
'name' => 'Primary Pool v2',
'add_ip' => '9.10.11.12',
'remove_ip' => '1.2.3.4',
'link_domain' => 'example.com',
'unlink_domain' => 'old.example.com',
]);
Manage IPs inside a pool
// Add a single IP to a pool
$mg->ips()->addIpToPool('my-pool-id', '9.10.11.12');
// Add multiple IPs at once
$mg->ips()->addIpsToPool('my-pool-id', ['9.10.11.12', '13.14.15.16']);
// Remove an IP from a pool
$mg->ips()->removeIpFromPool('my-pool-id', '1.2.3.4');
When a pool is linked to domains, adding or removing IPs propagates to all linked domains asynchronously after the API responds.
List domains linked to a pool
$response = $mg->ips()->getIpPoolDomains('my-pool-id', limit: 20);
foreach ($response->getDomains() as $domain) {
echo $domain['name'] . PHP_EOL;
}
// Paginate using the cursor from the previous response
if ($response->getNextPage()) {
$next = $mg->ips()->getIpPoolDomains('my-pool-id', page: $response->getNextPage());
}
Delete a pool
// Delete without replacement (pool must not be linked to any domains)
$mg->ips()->deleteDIPP('my-pool-id');
// Replace linked domains with a specific IP before deleting
$mg->ips()->deleteDIPP('my-pool-id', replacementIp: '1.2.3.4');
// Replace linked domains with another pool before deleting
$mg->ips()->deleteDIPP('my-pool-id', replacementPoolId: 'backup-pool-id');
Delegate a pool to a subaccount
// Grant a subaccount access to a pool
$mg->ips()->delegateIpPool('my-pool-id', 'sub-account-id');
// Revoke subaccount access
$mg->ips()->revokeDelegatedIpPool('my-pool-id', 'sub-account-id');
Analytics
$result = $mg->metrics()->loadMetrics([
'start' => 'Sun, 22 Dec 2024 00:00:00 +0000',
'end' => 'Sun, 29 Dec 2024 00:00:00 +0000',
'resolution' => 'day',
'dimensions' => ['time'],
'metrics' => ['accepted_count', 'delivered_count', 'clicked_rate', 'opened_rate'],
'include_aggregates' => true,
'include_subaccounts' => true,
]);
foreach ($result->getItems() as $item) {
echo $item['dimensions']['time'] . ': ' . $item['metrics']['delivered_count'] . ' delivered' . PHP_EOL;
}
Subaccounts
// Create a subaccount
$mg->subaccounts()->create('Marketing Team');
// List all subaccounts
$items = $mg->subaccounts()->index();
print_r($items->getItems());
// Enable / disable
$mg->subaccounts()->enable($subAccountId);
$mg->subaccounts()->disable($subAccountId);
Make API calls on behalf of a subaccount
// Pass the subaccount ID as the third argument to Mailgun::create()
$mg = Mailgun::create('your-api-key', 'https://api.mailgun.net', $subAccountId);
// All subsequent calls are scoped to that subaccount
$mg->messages()->send('example.com', [...]);
Response Handling
All API methods return typed model objects with IDE-friendly getters by default.
$domain = $mg->domains()->show('example.com');
foreach ($domain->getInboundDNSRecords() as $record) {
echo $record->getType() . ': ' . $record->getValue() . PHP_EOL;
}
Array responses
Prefer raw arrays? Inject ArrayHydrator:
use Mailgun\Hydrator\ArrayHydrator;
use Mailgun\HttpClient\HttpClientConfigurator;
$configurator = new HttpClientConfigurator();
$configurator->setApiKey('your-api-key');
$mg = new Mailgun($configurator, new ArrayHydrator());
$data = $mg->domains()->show('example.com');
// $data is now a plain associative array
Raw PSR-7 response
Need the raw response? Use NoopHydrator â note: no exceptions are thrown on non-200 responses when using this hydrator.
use Mailgun\Hydrator\NoopHydrator;
$mg = new Mailgun($configurator, new NoopHydrator());
$response = $mg->messages()->send('example.com', [...]);
// $response is a Psr\Http\Message\ResponseInterface
echo $response->getStatusCode();
Debugging
Route traffic through Mailgun's Postbin to inspect what the SDK sends:
use Mailgun\HttpClient\HttpClientConfigurator;
use Mailgun\Hydrator\NoopHydrator;
$configurator = new HttpClientConfigurator();
$configurator->setEndpoint('http://bin.mailgun.net/aecf68de'); // replace with your bin ID
$configurator->setApiKey('your-api-key');
$configurator->setDebug(true);
$mg = new Mailgun($configurator, new NoopHydrator());
$mg->messages()->send('example.com', [
'from' => 'alice@example.com',
'to' => 'bob@example.com',
'subject' => 'Debug test',
'text' => 'Checking what hits the wire.',
]);
Custom HTTP requests
$client = $mg->httpClient();
$client->httpGet('/v3/domains', ['limit' => 5]);
$client->httpPost('/v3/some/path', ['key' => 'value']);
$client->httpPut('/v3/some/path', ['key' => 'value']);
$client->httpDelete('/v3/some/path');
Framework Integration
| Framework | Package |
|---|---|
| Symfony | tehplague/swiftmailer-mailgun-bundle |
| Yii2 | katanyoo/yii2-mailgun-mailer |
| CakePHP | narendravaghela/cakephp-mailgun |
| Drupal | drupal/mailgun |
| Laravel | Built-in â see Laravel Mail docs |
Contributing
This is an open-source project under the MIT license, maintained by Mailgun and the community.
Running the tests
git clone git@github.com:mailgun/mailgun-php.git
cd mailgun-php
composer install
composer test
Ways to help
- Test the
dev-masterbranch and open issues for anything broken - Review open pull requests
- Add tests for untested endpoints
- Improve documentation and examples
Support
- Documentation: documentation.mailgun.com
- Issues: GitHub Issues
- Account support: app.mailgun.com/support
- More examples: doc/examples.md
Top Related Projects
Helps sending emails
The classic email sending library for PHP
Comprehensive mailing tools for PHP
The Official Twilio SendGrid PHP API Library
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot