定制 andywahyudi/multi-email-services 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

andywahyudi/multi-email-services

Composer 安装命令:

composer require andywahyudi/multi-email-services

包简介

A PHP library for handling multiple email service providers with failover support

README 文档

README

A robust PHP library for handling multiple email service providers with failover support. This library currently supports Mailgun, SendGrid, and Postmark with both API and SMTP implementations.

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 ):

# Default Provider Configuration
DEFAULT_EMAIL_PROVIDER=mailgun  # Options: mailgun, sendgrid, postmark

# 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:

  1. Attempt to send using the default provider first (if configured).
  2. If the default provider fails, it will automatically try other available providers.
  3. If all providers fail, it will throw an exception with detailed error messages.

Available Providers

  1. Mailgun

    • API and SMTP support.
    • Domain verification required.
    • Supports attachments, CC, and BCC.
  2. SendGrid

    • API and SMTP support.
    • Sender verification required.
    • Supports attachments, CC, and BCC.
  3. Postmark

    • API and SMTP support.
    • Server token required.
    • Supports attachments, CC, and BCC.

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.

andywahyudi/multi-email-services 适用场景与选型建议

andywahyudi/multi-email-services 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 andywahyudi/multi-email-services 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 andywahyudi/multi-email-services 我们能提供哪些服务?
定制开发 / 二次开发

基于 andywahyudi/multi-email-services 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-18