Download the PHP package anikninja/mail-mapper without Composer
On this page you can find all versions of the php package anikninja/mail-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anikninja/mail-mapper
More information about anikninja/mail-mapper
Files in anikninja/mail-mapper
Package mail-mapper
Short Description A Laravel package for dynamic, configurable email mapping and notification management.
License MIT
Homepage https://github.com/anikninja/mail-mapper
Informations about the package mail-mapper
π¬ Mail Mapper for Laravel
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:
Publish package resources:
Run migrations:
βοΈ Configuration
Config file location:
Example:
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:
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:
π§ 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:
| 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)
β Using Helper Function
Attachments
Pass attachments via the $extra parameter or the model context when calling notifyByMapping(...).
Supported formats:
-
In-memory attachment (array with
filename,content,mime): -
File path (server file path or stored temporary file):
- Uploaded file (from a request):
Example usage:
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:
π¨ Email Template
Default template:
To customize:
π§ͺ Queue Requirement
This package uses Laravel queues.
Run worker:
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)
Response example (201 Created):
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.