andywahyudi / multi-email-services
A PHP library for handling multiple email service providers with failover support
Requires
- php: ^8.1
- getbrevo/brevo-php: ^5.0
- mailgun/mailgun-php: ^3.5
- nyholm/psr7: ^1.8
- phpmailer/phpmailer: ^6.9
- resend/resend-php: ^1.17
- sendgrid/sendgrid: ^8.0
- symfony/http-client: ^5.4|^6.0
- wildbit/postmark-php: ^4.0
Requires (Dev)
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A robust PHP library for handling multiple email service providers with failover support. It supports Mailgun, SendGrid, Postmark, Brevo, Resend, SendLayer, SMTP.com, Gmail SMTP, and Microsoft SMTP.
Features
- Multiple email service provider support
- Automatic failover handling
- Support for both API and SMTP sending methods
- Attachment handling
- CC and BCC support
- HTML email support
- Easy configuration
Requirements
- PHP 8.1 or higher
- Composer
Installation
composer require andywahyudi/multi-email-services
Configuration
Create a .env file in your project root (copy from .env.example ):
# Brevo Configuration (transactional email API) BREVO_API_KEY=your_brevo_api_key BREVO_FROM_EMAIL=noreply@yourdomain.com BREVO_FROM_NAME=Your App Name # Resend Configuration RESEND_API_KEY=re_your_resend_api_key RESEND_FROM_EMAIL=noreply@yourdomain.com RESEND_FROM_NAME=Your App Name # SendGrid Configuration SENDGRID_API_KEY=your_sendgrid_api_key_here SENDGRID_USE_SMTP=false SENDGRID_FROM_ADDRESS=your_verified_sender@domain.com # Mailgun Configuration MAILGUN_API_KEY=your_mailgun_api_key_here MAILGUN_DOMAIN=your_domain_here MAILGUN_FROM_ADDRESS=your_sender@your_domain.com MAILGUN_USE_SMTP=false # Postmark Configuration POSTMARK_SERVER_TOKEN=your_postmark_server_token_here POSTMARK_FROM_ADDRESS=your_sender@your_domain.com POSTMARK_USE_SMTP=false
Usage
Basic Usage with Default Provider
use MultiEmailServices\Library\EmailServiceManager; use MultiEmailServices\Library\Providers\MailgunService; use MultiEmailServices\Library\Providers\SendGridService; use MultiEmailServices\Library\Providers\PostmarkService; // Initialize the Email Service Manager (will use DEFAULT_EMAIL_PROVIDER from .env) $emailManager = new EmailServiceManager(); // Configure and add providers $mailgunConfig = [ 'api_key' => getenv('MAILGUN_API_KEY'), 'domain' => getenv('MAILGUN_DOMAIN'), 'from_address' => getenv('MAILGUN_FROM_ADDRESS') ]; $sendGridConfig = [ 'api_key' => getenv('SENDGRID_API_KEY'), 'from_address' => getenv('SENDGRID_FROM_ADDRESS') ]; $postmarkConfig = [ 'api_key' => getenv('POSTMARK_SERVER_TOKEN'), 'from_address' => getenv('POSTMARK_FROM_ADDRESS') ]; // Add providers to the manager $emailManager ->addProvider(new MailgunService($mailgunConfig)) ->addProvider(new SendGridService($sendGridConfig)) ->addProvider(new PostmarkService($postmarkConfig)); // Optionally change default provider at runtime $emailManager->setDefaultProvider('sendgrid'); // Get current default provider $currentProvider = $emailManager->getDefaultProvider(); // Get list of available providers $availableProviders = $emailManager->getAvailableProviders(); // Send email (will use default provider first) try { $emailManager->send( 'recipient@example.com', 'Test Subject', '<h1>Hello World!</h1>', ['from' => 'sender@yourdomain.com'] ); } catch (\Exception $e) { echo "Failed to send email: " . $e->getMessage(); }
Using SMTP
To use SMTP instead of API, update your configuration:
$mailgunConfig = [ 'use_smtp' => true, 'smtp_host' => 'smtp.mailgun.org', 'smtp_port' => 587, 'smtp_username' => 'your_smtp_username', 'smtp_password' => 'your_smtp_password', 'from_address' => 'your_sender@your_domain.com' ];
Provider Failover
The library will:
- Attempt to send using the default provider first (if configured).
- If the default provider fails, it will automatically try other available providers.
- If all providers fail, it will throw an exception with detailed error messages.
Failover is best-effort: a timeout may happen after a provider accepted the email, so trying another provider can result in duplicate delivery. For critical or high-volume sending, coordinate retries and idempotency in your application's queue/outbox.
Available Providers
-
Mailgun
- API and SMTP support.
- Domain verification required.
- Supports attachments, CC, and BCC.
-
SendGrid
- API and SMTP support.
- Sender verification required.
- Supports attachments, CC, and BCC.
-
Postmark
- API and SMTP support.
- Server token required.
- Supports attachments, CC, and BCC.
-
Brevo
- Official Brevo PHP SDK for transactional email API sending; optional SMTP mode is also supported.
- Requires a Brevo API key and a verified sender.
-
Resend
- Official Resend PHP SDK for API sending.
- Requires a Resend API key and a verified sender/domain.
- Supports HTML and plain-text bodies, CC/BCC, reply-to, and file attachments.
-
SendLayer — API and SMTP sending.
-
SMTP.com — SMTP sending.
-
Gmail — SMTP sending with an App Password.
-
Microsoft — SMTP sending for Outlook.com and Office 365.
Error Handling
The library implements a failover system. If one provider fails, it automatically tries the next available provider. If all providers fail, an exception is thrown with detailed error messages from each provider.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.