anikninja/mail-mapper
Composer 安装命令:
composer require anikninja/mail-mapper
包简介
A Laravel package for dynamic, configurable email mapping and notification management.
README 文档
README
Mail Mapper is a standalone Laravel package that provides a dynamic, configurable email mapping and notification system.
It allows administrators or developers to define who receives which emails for specific module actions — without changing application code.
This package is ideal for ERP, CRM, and enterprise Laravel applications where email recipients frequently change.
✨ Features
- Module / Menu / Task based email mapping
- Dynamic To / CC email configuration from database
- Queue-based email dispatching
- Blade-based HTML email template
- Raw mail fallback for strict SMTP servers
- Easy integration using Trait or Helper
- Publishable config, migrations, and views
📦 Installation
Install via Composer:
composer require anikninja/mail-mapper
Publish package resources:
php artisan vendor:publish --provider="AnikNinja\MailMapper\MailMapperServiceProvider"
Run migrations:
php artisan migrate
⚙️ Configuration
Config file location:
config/mail-mapper.php
Example:
return [ 'default_from' => [ 'address' => env('MAIL_FROM_ADDRESS', 'no-reply@example.com'), 'name' => env('MAIL_FROM_NAME', 'No Reply'), ], 'use_raw_fallback' => env('MAIL_MAPPER_USE_RAW_FALLBACK', true), 'enable_logging' => env('MAIL_MAPPER_ENABLE_LOGGING', true), 'user_model' => config('auth.providers.users.model', \App\Models\User::class), // Change default as needed ];
Authorization Configuration
The package provides configurable authorization options for the API and management UI. After publishing config/mail-mapper.php you can set the following under the authorization key (or via env variables):
roles— array of role names allowed to manage mappings (supportsspatie/laravel-permissionmethods likehasRole/hasAnyRole). Can be set viaMAIL_MAPPER_AUTH_ROLES=admin,super-admin.permissions— array of permission/gate names to check via$user->can('permission')(e.g.['email-mapping-configure']).allow_super_admin— boolean fallback to allowsuper-adminrole orsuper-admin-onlypermission (defaults totrue).default_allow— boolean default if no roles/permissions match (defaults tofalse).
Example configuration:
'authorization' => [ 'roles' => ['admin','super-admin'], 'permissions' => ['email-mapping-configure'], 'allow_super_admin' => true, 'default_allow' => false, ],
Notes:
- The package registers a default
EmailMappingPolicythat respects these settings. Host applications can override the policy or adjust these config values after publishing. - This approach makes authorization flexible across different auth/permission packages and app conventions.
🗒️ ENV Example:
MAIL_FROM_ADDRESS="no-reply@example.com" # Default "from" email address for outgoing emails MAIL_FROM_NAME="No Reply" # Default "from" name for outgoing emails MAIL_MAPPER_USE_RAW_FALLBACK=true # Enable/disable raw email sending fallback (true/false) MAIL_MAPPER_ENABLE_LOGGING=true # Enable/disable logging of email attachment info (true/false) MAIL_MAPPER_API_PREFIX="api" # The route prefix for all API routes. MAIL_MAPPER_API_VERSION="v1" # Optional version segment for the API routes. It can be set as null MAIL_MAPPER_API_PER_PAGE=10 # Default pagination size for listing endpoints MAIL_MAPPER_API_MAX_PER_PAGE=100 # Maximum allowed pagination size to prevent abuse. MAIL_MAPPER_AUTH_ROLES=admin,super-admin # User roles that are allowed to manage email mappings. (e.g. admin,super-admin). MAIL_MAPPER_ALLOW_SUPER_ADMIN=true # Allow users with 'super-admin' role or permission by default.
🧠 Core Concepts
Concept Description:
- Module High-level system area (Sales, SCM, Support)
- Menu Feature name (Lead Generation, Purchase Order)
- Task Action name (Create, Update, Delete)
- To / CC Dynamic email recipients
- Body Email content (HTML supported)
🗄️ Database Structure
Email mappings are stored in:
email_mappings
| Column | Description |
|---|---|
| module | Module name |
| menu | Menu name |
| task | Task/action |
| to | Comma-separated email list |
| cc | Optional CC emails |
| body | Email body (HTML supported) |
| meta | Holding placeholder attributes |
🚀 Usage
✅ Using Trait (Recommended)
use AnikNinja\MailMapper\Traits\NotifiesByEmailMapping; class LeadController extends Controller { use NotifiesByEmailMapping; public function store(Request $request) { // Business logic... $this->notifyByMapping( module: 'Sales', menu: 'Lead Generation', task: 'Create', modelOrContext: $lead, // Lead Model Object or associative array providing context for placeholders. extra: [ 'custom_note' => 'Urgent lead created' ], useRaw: false ); } }
✅ Using Helper Function
send_mail_mapping( 'Sales', 'Lead Generation', 'Update', [ 'customer_name' => 'Customer 1', 'customer_address' => 'Customer Address 1' ] );
Attachments
Pass attachments via the $extra parameter or the model context when calling notifyByMapping(...).
Supported formats:
- In-memory attachment (array with
filename,content,mime):
['attachments' => [ [ 'filename' => 'file.pdf', 'content' => file_get_contents($path), 'mime' => 'application/pdf', ], ]]
- File path (server file path or stored temporary file):
['attachments' => ['/full/path/to/file.pdf']]
- Uploaded file (from a request):
['attachments' => [$request->file('upload')]]
Example usage:
$this->notifyByMapping( 'Sales', 'Leads', 'Create', $model, ['attachments' => ['/path/to/invoice.pdf']] );
Notes:
- The trait will normalize attachments and prefer path-based
attach()to avoid loading large files into memory. - Prefer passing file paths or
UploadedFileinstances for large files.
Parameters
| Name | Type | Description |
|---|---|---|
module |
string | The module name (e.g., 'Sales'). Supports wildcard (*) to match any module. |
menu |
string | The feature or menu name (e.g., 'Lead Generation'). Supports wildcard (*) for flexible mapping. |
task |
string | The action or event name (e.g., 'Create', 'Update'). Supports wildcard (*) for generic or fallback templates. |
modelOrContext |
Model | array |
An Eloquent model or associative array providing context for placeholders. |
extra |
array (optional) | Additional data (like: url, attachments) to merge into the context. |
useRaw |
bool (optional) | If true, bypasses mapping and sends a raw email directly. |
🔹 Wildcard Matching
The Module, Menu, and Task parameters support the * wildcard to allow flexible mapping rules:
| Example | Description |
|---|---|
module: '*' |
Matches any module (used as a global fallback). |
menu: '*' |
Matches any feature or menu within the specified module. |
task: '*' |
Matches all actions or events for a given module and menu. |
module: 'Sales', menu: '*', task: 'Create' |
Matches all “Create” actions under the Sales module, regardless of feature. |
Wildcard support allows you to define general-purpose email templates that apply to multiple actions or modules — reducing redundancy and centralizing notification management.
⚙️ How It Works
1. Context Extraction
Builds a unified context array from the provided model or array, merging any extra data.
2. Meta Placeholder Management
Extracts all placeholders used in templates and stores them in the mapping’s meta field if not already present.
3. Recipient & Template Resolution
Uses EmailMappingService to resolve recipients, subject, and body dynamically, applying context placeholders.
4. Email Dispatch
Dispatches the fully rendered email through SendEmailNotificationJob for queued or asynchronous delivery.
🗒️ Notes
- Ensure your
EmailMappingmodel and database table are properly configured to store mapping definitions and meta placeholders. - Placeholders in templates (e.g.,
{client_name}) are automatically replaced with values from the provided context. {actor_name}and{actor_email}are injected automatically when an authenticated user exists.
📨 Email Sending Strategy
The package automatically decides how to send email:
- Mailable (HTML template)
- Raw mail fallback if SMTP server rejects HTML mailables
You can force raw mode:
SendEmailNotificationJob::dispatch([ 'to' => ['admin@example.com'], 'subject' => 'System Update', 'body' => '<p>Update completed</p>', 'use_raw' => true, ]);
🎨 Email Template
Default template:
resources/views/mail/dynamic_notification.blade.php
To customize:
php artisan vendor:publish --tag=views
🧪 Queue Requirement
This package uses Laravel queues.
Run worker:
php artisan queue:work
Recommended drivers:
- database
- redis
- supervisor (production)
⚙️ API
The package exposes a simple CRUD API for managing email mappings. Routes are published under the configured API prefix (default: /api) and are protected by middleware defined in config/mail-mapper.php.
Default endpoints (prefix may include version if configured):
- GET
/email-mappings— List mappings. Supports?per_page=pagination. - GET
/email-mappings/{id}— Get a single mapping. - POST
/email-mappings— Create mapping (returns 201). - PUT
/email-mappings/{id}— Update mapping. - DELETE
/email-mappings/{id}— Delete mapping.
Authentication & Authorization:
- Routes are protected by the middleware defined in
config('mail-mapper.api.middleware')(default['api','auth:api']). - The package provides a default
EmailMappingPolicy(registered by the service provider). Host applications should register permissions (for exampleemail-mapping-configure) or override the policy to customize access control.
Example: Create mapping (cURL)
curl -X POST https://your-app.test/api/email-mappings \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"module":"Sales",
"menu":"Leads",
"task":"Create",
"to":["ops@example.com"],
"cc":["manager@example.com"],
"subject":"New lead",
"body":"<p>Hello {client_name}</p>",
"is_active": true
}'
Response example (201 Created):
{
"message": "Email Mapping created successfully.",
"data": {
"id": 1,
"module": "Sales",
"menu": "Leads",
"task": "Create",
"to": ["ops@example.com"],
"cc": ["manager@example.com"],
"subject": "New lead",
"body": "<p>Hello {client_name}</p>",
"is_active": true,
"meta": [],
"last_updated_by": null,
"created_at": "2025-10-20T12:34:56Z",
"updated_at": "2025-10-20T12:34:56Z"
}
}
Notes:
-
The list endpoint supports
?per_page=; the default and maximum values are configurable viaconfig('mail-mapper.api.per_page')andconfig('mail-mapper.api.max_per_page'). -
The package includes an OpenAPI 3 specification at the project root:
openapi.yaml. -
To import into Postman: open Postman -> Import -> File -> select
openapi.yaml. -
Protect the endpoints using your preferred auth middleware (Sanctum, Passport, or
auth:api) by publishing and editingconfig/mail-mapper.php. -
Consider applying rate-limiting middleware (
throttle) in your host application for public APIs.
🧰 Troubleshooting
| Issue | Solution |
|---|---|
| Mail not sent | Check SMTP config |
| Job not running | Run queue worker |
| Template not rendering | Publish views |
| SMTP rejects HTML | Enable use_raw |
🧾 Summary
Mail Mapper is a standalone Laravel package that provides a dynamic, database-driven email notification system for modular applications.
It enables module, menu, and task-based email routing without hardcoding recipient addresses in application code.
Built for enterprise-scale systems, Mail Mapper allows administrators to manage email recipients and content from the UI, while developers trigger notifications using a simple trait or helper. The package supports queued delivery, customizable HTML templates, and a raw SMTP fallback to ensure reliable email sending across different mail servers.
anikninja/mail-mapper 适用场景与选型建议
anikninja/mail-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mail」 「email」 「notifications」 「laravel」 「mail-mapping」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anikninja/mail-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anikninja/mail-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anikninja/mail-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova package that adds a notification feed in your Nova app.
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
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.
Captures outgoing SMS notifications
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-18