darvis/mailtrap 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

darvis/mailtrap

Composer 安装命令:

composer require darvis/mailtrap

包简介

A Laravel package for Mailtrap integration

README 文档

README

A powerful Laravel package for Mailtrap integration: email validation, automatic logging of outgoing mail, a Mailtrap-style inbox UI and a CLI health-check command.

Laravel PHP

🚀 Quick Start

use Darvis\Mailtrap\Models\EmailValidation;

// Validate a single email address (returns null if valid, error message if invalid)
$errorMessage = EmailValidation::validateEmail('user@example.com');
$isValid = $errorMessage === null;

// Bulk validation of multiple email addresses
$emails = ['user1@test.com', 'user2@example.com', 'user3@invalid.domain'];
$result = EmailValidation::bulkValidationStatus($emails);

echo "Valid emails: " . $result['valid'];
echo "Invalid emails: " . $result['invalid'];

📚 Documentation

For complete documentation and extensive examples:

📖 Go to Documentation →

Specific topics:

✨ Features

🎯 Email Validation

  • Format Validation - Checks basic email format
  • MX Record Verification - Verifies domain mail servers
  • IP Validation - Checks if MX records point to valid IPs
  • Bulk Validation - Efficient validation of multiple email addresses
  • Laravel Collections - Powerful filtering and data manipulation
  • Status Tracking - Maintains validation history

🔧 Integration & Performance

  • Database Caching - Fast lookups of previously validated emails
  • Automatic Registration - Laravel package discovery
  • Event Listeners - Automatic validation via Laravel mail events
  • Mailer-independent - Logging & validation work with any transport (Mailtrap, Microsoft Graph, SES, …)
  • Rate Limiting - Built-in API rate limiting
  • Webhook Support - Mailtrap callback support

📊 Monitoring & Logging

  • Mail Logging - Automatic logging of outgoing emails
  • Detailed Tracking - Comprehensive validation reporting
  • Status Codes - HTTP-like status codes for categorization
  • Configurable - Extensive configuration options

📬 Inbox UI & Tooling

  • Mailtrap-style Inbox - Livewire/Flux page to inspect every outgoing email
  • Search & Filters - Filter by status (sent, pending, failed, blocked) and search recipient/sender/subject
  • Send Test Mail - Trigger a test email straight from the inbox
  • Delete & Cleanup - Remove a single log or purge logs older than the retention period
  • mailtrap:test Command - CLI health-check with CI-friendly exit codes

Author

Arvid de Jong
Email: info@arvid.nl

📦 Installation

Install the package via Composer:

composer require darvis/mailtrap

⚙️ Configuration

The package is automatically registered via Laravel's package discovery.

Database Setup

Migrations are automatically loaded. To publish migrations to your application:

php artisan vendor:publish --tag=mailtrap-migrations
php artisan migrate

Or simply run php artisan migrate - migrations are loaded automatically.

Configuration

Publish the config file for custom settings:

php artisan vendor:publish --tag=mailtrap-config

This creates a config/manta_mailtrap.php file with configuration for:

  • API Settings - Mailtrap API token and base URL
  • Email Validation - Validation settings and caching
  • Mail Logging - Log settings for outgoing emails (incl. cleanup_after_days retention)
  • Webhook - Webhook configuration and signature verification
  • Rate Limiting - API rate limiting settings
  • Inbox UI - Route, middleware, layout and page size for the inbox

Environment Variables:

MAILTRAP_API_TOKEN=your_api_token_here
MAILTRAP_VALIDATION_ENABLED=true
MAILTRAP_LOGGING_ENABLED=true
MAILTRAP_CLEANUP_AFTER_DAYS=30
MAILTRAP_WEBHOOK_ENABLED=true
MAILTRAP_WEBHOOK_SECRET=your_webhook_secret

# Inbox UI
MAILTRAP_UI_ENABLED=true
MAILTRAP_UI_ROUTE=mailtrap
MAILTRAP_UI_MIDDLEWARE=web,auth
MAILTRAP_UI_LAYOUT=components.layouts.app
MAILTRAP_UI_PER_PAGE=25

💡 Usage Examples

Basic Usage

// Via the Mailtrap service
$mailtrap = app(Darvis\Mailtrap\Services\MailtrapService::class);

// Or via the alias
$mailtrap = app('mailtrap');

// Email validation
$isValid = $mailtrap->validateEmail('test@example.com');

Laravel Collections Filtering

use Darvis\Mailtrap\Models\EmailValidation;

$emails = ['valid@test.com', 'invalid@spam.com', 'unknown@new.com'];
$result = EmailValidation::bulkValidationStatus($emails);

// Filter invalid emails with Laravel Collections
$invalidEmails = collect($result['details'])
    ->filter(fn($details) => $details['status'] !== 'valid')
    ->keys()
    ->toArray();

// Group by status
$grouped = collect($result['details'])
    ->groupBy('status')
    ->map(fn($group) => $group->keys()->toArray());

📖 More examples in the documentation →

📬 Inbox UI

A Mailtrap-style inbox to inspect outgoing mail, built with Livewire and Flux UI.

Requirements: the host application must have livewire/livewire and livewire/flux installed. When Livewire is absent (or MAILTRAP_UI_ENABLED=false) the UI registration is skipped automatically — the rest of the package keeps working.

Once enabled, the inbox lives at the configured route (default /mailtrap) and offers:

  • A paginated list of all logged mail with status badges (sent, pending, failed, blocked)
  • Search by recipient, sender or subject, and filter by status
  • A detail view per message (message id, status, error, source file/line, linked model)
  • A Send test mail action straight from the inbox
  • Delete a single log, or Cleanup logs older than logging.cleanup_after_days

Configuration

All UI settings are driven by environment variables (see the ui section in config/manta_mailtrap.php):

MAILTRAP_UI_ENABLED=true
MAILTRAP_UI_ROUTE=mailtrap
MAILTRAP_UI_MIDDLEWARE=web,auth        # comma-separated middleware stack
MAILTRAP_UI_LAYOUT=components.layouts.app
MAILTRAP_UI_PER_PAGE=25

Protect the route with appropriate middleware — the inbox exposes recipients, subjects and error details. For example MAILTRAP_UI_MIDDLEWARE=web,auth (or a custom staff/admin middleware).

Tailwind / Flux

Add the package views to your Tailwind sources so its utility classes are compiled (Tailwind v4 example in resources/css/app.css):

@source '../../vendor/darvis/mailtrap/resources/views/**/*.blade.php';

Optionally publish the views to override them in your application:

php artisan vendor:publish --tag=mailtrap-views

📮 Mail Transport vs. Mailtrap

This package hooks into Laravel's MessageSending / MessageSent events, so it works independently of the mailer (transport) you send through. Sending via Mailtrap's SMTP, Microsoft Graph, Amazon SES or any other Laravel mailer all flow through the same pipeline — pre-send validation and MailLog logging happen for every transport.

The webhook flow is the one exception: delivery, open, click, bounce, spam and reject events are reported only by Mailtrap. If you send through another transport (e.g. microsoft-graph), local logging and validation still work, but there is no post-delivery feedback to confirm or invalidate addresses.

Capability Any mailer Mailtrap only
Pre-send validation (format / MX / blocklist)
Outgoing mail logging (MessageSending / MessageSent)
Inbox UI & mailtrap:test health check
Delivery / open / click confirmation → markAsValid ✅ (webhook)
Bounce / spam / reject → markAsInvalid ✅ (webhook)

In practice: you can route production mail through Microsoft Graph and still get full logging and the inbox UI. To also keep the validation feedback loop — auto-confirming good addresses and flagging bounces — the delivery events must come from Mailtrap.

🩺 Health Check Command

Send a test email and report the result. The command returns exit code 0 on success and 1 on failure, which makes it suitable for CI pipelines and uptime monitoring.

php artisan mailtrap:test you@example.com

# Use a specific mailer instead of the default
php artisan mailtrap:test you@example.com --mailer=microsoft-graph

It sends the mail, reads back the corresponding mail log and prints a summary table (message id, sender, recipient, subject, status, error message). A recorded non-success status (e.g. a blocked recipient) is also treated as a failure.

🔗 API Endpoints

Webhook

The package automatically includes a webhook endpoint:

  • Endpoint: POST /api/webhooks/mailtrap
  • Route name: webhooks.mailtrap

The webhook is automatically registered and accepts external calls from Mailtrap.

🛠️ Development

This package is actively developed with focus on:

  • Performance optimization
  • Comprehensive email validation
  • Automatic blocking of invalid emails
  • Comprehensive mail logging
  • Inbox UI and CLI tooling for inspecting and testing outgoing mail

License

This package is open-sourced software licensed under the MIT license.

darvis/mailtrap 适用场景与选型建议

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

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

围绕 darvis/mailtrap 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-25