定制 salines/cakephp-mail-interceptor 二次开发

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

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

salines/cakephp-mail-interceptor

Composer 安装命令:

composer require --dev salines/cakephp-mail-interceptor

包简介

CakePHP plugin to intercept and redirect outgoing emails to a specified address. Useful for development and staging environments.

README 文档

README

CI License: MIT CakePHP 5.x PHP 8.1+

A CakePHP 5.x plugin that intercepts all outgoing emails and redirects them to a specified address. Perfect for development and staging environments where you want to test email functionality without sending emails to real users.

Why Use This Plugin?

When developing or testing applications that send emails, you need a way to prevent emails from reaching real users. There are several approaches:

Paid services like Mailtrap or Mailosaur work great but require subscriptions and external dependencies.

Local tools like Mailpit, MailHog, or MailCatcher are excellent free alternatives, but they require local installation and configuration - which isn't always possible in shared hosting environments, Docker-less setups, or restricted infrastructure.

This plugin offers a zero-infrastructure solution:

  • No additional services to install or maintain
  • Works with your existing email transport (SMTP, Mailgun, SES, etc.)
  • Simple configuration change - just wrap your existing transport
  • Ideal for shared staging environments where installing local tools isn't an option
  • Perfect for quick local development without setting up additional services

Features

  • Redirects all outgoing emails to a single address
  • Preserves original recipient information in email headers (X-Original-To, X-Original-Cc, X-Original-Bcc)
  • Adds configurable subject prefix to identify intercepted emails
  • Optionally includes original recipients in subject line
  • Logs all intercepted emails
  • Works with any underlying CakePHP mail transport (SMTP, Mailgun, etc.)

Requirements

  • PHP 8.1+
  • CakePHP 5.0+

Installation

composer require salines/cakephp-mail-interceptor

Load the plugin in your src/Application.php:

use CakeMailInterceptor\CakeMailInterceptorPlugin;

public function bootstrap(): void
{
    parent::bootstrap();
    $this->addPlugin(CakeMailInterceptorPlugin::class);
}

Configuration

Configure the transport in your config/app_local.php:

'EmailTransport' => [
    // Your real transport (used in production)
    'smtp' => [
        'className' => \Cake\Mailer\Transport\SmtpTransport::class,
        'host' => 'smtp.example.com',
        'port' => 587,
        'username' => 'user@example.com',
        'password' => 'secret',
        'tls' => true,
    ],

    // Intercept transport (used in development/staging)
    'default' => [
        'className' => \CakeMailInterceptor\Mailer\Transport\InterceptTransport::class,
        'transport' => 'smtp', // The underlying transport to use
        'to' => 'dev@example.com', // Where all emails will be redirected
        'subjectPrefix' => 'DEV', // Optional: tag for subject line
        'includeOriginalInSubject' => true, // Optional: add original recipients to subject
        'logInterceptions' => true, // Optional: log intercepted emails
    ],
],

Configuration Options

Option Type Default Description
transport string required Name of the underlying transport to use for sending
to string required Email address where all emails will be redirected
subjectPrefix string 'INTERCEPTED' Tag used in subject line prefix
includeOriginalInSubject bool true Whether to include original recipients in subject prefix
logInterceptions bool true Whether to log intercepted emails

How It Works

When an email is sent through the InterceptTransport:

  1. Original recipients (To, Cc, Bcc) are saved to custom headers
  2. All recipients are replaced with the configured to address
  3. Subject is modified with prefix and/or original recipients
  4. Email is sent using the underlying transport
  5. Interception is logged (if enabled)

Example

Original email:

  • To: user@example.com
  • Cc: manager@example.com
  • Subject: Your Order Confirmation

Intercepted email:

  • To: dev@example.com
  • Subject: [DEV: user@example.com] Your Order Confirmation
  • Header X-Original-To: user@example.com
  • Header X-Original-Cc: manager@example.com

Recommended: Use Plus-Addressed Emails

We recommend using plus-addressed (subaddressed) emails for intercepted mail:

'to' => 'dev+projectname@example.com',
// or with environment identifier
'to' => 'dev+myapp-staging@example.com',

Benefits of plus addressing:

  • Easy filtering - Create inbox rules to automatically sort intercepted emails by project or environment
  • Quick identification - Instantly see which project/environment an email came from
  • Single inbox - Use one email account for all projects without mixing emails
  • No extra accounts - No need to create separate email addresses for each project

Most email providers support plus addressing, including Gmail, Outlook, ProtonMail, Fastmail, and others.

Environment-Based Configuration

A common pattern is to use the intercept transport only in non-production environments:

// config/app_local.php
'EmailTransport' => [
    'smtp' => [
        'className' => \Cake\Mailer\Transport\SmtpTransport::class,
        // ... smtp config
    ],
    'default' => env('APP_ENV') === 'production'
        ? [
            'className' => \Cake\Mailer\Transport\SmtpTransport::class,
            // ... production config
        ]
        : [
            'className' => \CakeMailInterceptor\Mailer\Transport\InterceptTransport::class,
            'transport' => 'smtp',
            'to' => 'dev@example.com',
        ],
],

Testing

composer install
composer test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License. See LICENSE for details.

salines/cakephp-mail-interceptor 适用场景与选型建议

salines/cakephp-mail-interceptor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mail」 「testing」 「email」 「cakephp」 「development」 「cakephp-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 salines/cakephp-mail-interceptor 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-16