spatie/email-concealer-cli
CLI tool to conceal email addresses in files by replacing their domains. Ideal for sanitizing production data like MySQL dumps before using them locally, so you can share or test with realistic data without storing real addresses.
Installation:
composer require spatie/email-concealer-cli --dev
Ensure the package is in require-dev for local/dev environments only.
Basic Usage: Run the CLI command on a file (e.g., MySQL dump, Markdown, or text file):
php artisan conceal:emails path/to/your/file.txt
user@example.com → user@concealed.example.com).STDOUT by default; use --output=path/to/output.txt to save to a file.First Use Case:
php artisan conceal:emails production_dump.sql --output=local_dump.sql
README.md) to avoid exposing real emails in docs:
php artisan conceal:emails README.md
config/email-concealer.php (auto-generated on first run) for customization (e.g., replacement domains, regex patterns).php artisan to see available commands:
php artisan conceal:emails --help
CI/CD Integration:
deploy:prepare script to conceal emails in artifacts before deployment:
# In .github/workflows/deploy.yml
- name: Conceal emails in dump
run: php artisan conceal:emails production_dump.sql --output=local_dump.sql
--dry-run to test without modifying files:
php artisan conceal:emails file.txt --dry-run
Laravel Artisan Tasks:
// app/Console/Commands/ConcealEmailsInProject.php
public function handle() {
$files = [$this->laravel->basePath('README.md'), $this->laravel->basePath('data.sql')];
foreach ($files as $file) {
Artisan::call('conceal:emails', ['path' => $file]);
}
}
File Processing Pipelines:
sed, grep) for complex workflows:
php artisan conceal:emails input.txt | grep "concealed" > filtered.txt
if (app()->environment('production')) {
return;
}
Artisan::call('conceal:emails', ['path' => $file]);
config/email-concealer.php:
'replacement_domain' => 'dev.example.com',
'email_regex' => '/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i',
--backup to create a .bak file before modifying:
php artisan conceal:emails file.txt --backup
Performance with Large Files:
--chunk-size (if supported) or process in smaller batches.split -l 100000 big_dump.sql dump_).False Positives in Regex:
user@sub.domain.co.uk or user@192.168.1.1). Test with --dry-run first.email_regex in config or use a stricter pattern like:
'/[a-z0-9._%+-]+@(?:[a-z0-9-]+\.)+[a-z]{2,}/i'
File Encoding Issues:
iconv -f ISO-8859-1 -t UTF-8 input.sql > utf8_input.sql
php artisan conceal:emails utf8_input.sql
Overwriting Originals:
--output to avoid accidental overwrites.--backup or --dry-run in scripts.Case Sensitivity:
User@Example.COM) might not trigger. Test thoroughly.php artisan conceal:emails file.txt --verbose
php artisan conceal:emails file.txt > conceal.log 2>&1
hexdump or od to inspect files for non-printable characters causing issues:
hexdump -C file.txt | head
Custom Concealer Logic:
spatie/email-concealer library by publishing its config:
php artisan vendor:publish --provider="Spatie\EmailConcealer\EmailConcealerServiceProvider"
Concealer class to add custom rules (e.g., whitelist domains).Pre/Post-Processing Hooks:
registering and registered events in the EmailConcealerServiceProvider to add logic before/after concealment.Parallel Processing:
parallel helper or spatie/array-to-xml for batch processing:
use Spatie\ArrayToXml\ArrayToXml;
$files = ['file1.txt', 'file2.txt'];
parallel($files, function ($file) {
Artisan::call('conceal:emails', ['path' => $file]);
});
config/email-concealer.php on first run. Ensure this file is committed to version control if shared across environments.config() helper to dynamically set options:
$replacementDomain = config('services.local_email_domain', 'dev.example.com');
config(['email-concealer.replacement_domain' => $replacementDomain]);
enabled: false in the config to bypass concealment entirely (useful for feature flags).How can I help you explore Laravel packages today?