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
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:
- Original recipients (To, Cc, Bcc) are saved to custom headers
- All recipients are replaced with the configured
toaddress - Subject is modified with prefix and/or original recipients
- Email is sent using the underlying transport
- 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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 salines/cakephp-mail-interceptor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
The PHP SDK for Checkmango
Mail libraries used by Zimbra Api
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 41
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-16