This package provides a simple way to mask email addresses in your Laravel applications.
composer require Tchilly/EmailMasking<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tchilly\EmailMasking\Traits\HasMaskedEmail;
class User extends Authenticatable
{
use HasMaskedEmail;
// Make sure to add 'email_masked' to your $appends array
// if you want it included in your model's array/JSON form
protected $appends = ['email_masked'];
}Then you can access the masked email as a property:
$user = User::first();
echo $user->email_masked; // j***@example.comYou can also use the method with custom options:
$user = User::first();
echo $user->getMaskedEmail(); // Default masking (same as email_masked)
echo $user->getMaskedEmail(null, 3); // With max 3 asterisks
echo $user->getMaskedEmail('[email protected]'); // Custom email with default maskingYou can also use the trait's static method directly:
use Tchilly\EmailMasking\Traits\HasMaskedEmail;
$maskedEmail = HasMaskedEmail::maskEmail('[email protected]');
echo $maskedEmail; // j******@example.com
// Customize the maximum number of asterisks
$maskedEmail = HasMaskedEmail::maskEmail('[email protected]', 3);
echo $maskedEmail; // j***@example.comYou can customize the default masking behavior by overriding the emailMasked method in your model:
// In your model
protected function emailMasked(): Attribute
{
return new Attribute(
get: fn () => static::maskEmail($this->email, 3) // Always use max 3 asterisks
);
}Magnus Vike [email protected]
This package is open-sourced software licensed under the MIT license.