PHP code example of plin-code / laravel-email-fixer
1. Go to this page and download the library: Download plin-code/laravel-email-fixer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
plin-code / laravel-email-fixer example snippets
use PlinCode\LaravelEmailFixer\Facades\EmailFixer;
// Fix a single email
$fixed = EmailFixer::fix('user@gmail,com');
// "[email protected]"
// Fix or get null if unfixable
$fixed = EmailFixer::fixOrNull('not-an-email');
// null
// Check if input is garbage before attempting a fix
$isGarbage = EmailFixer::isGarbage('asdf');
// true
// Get a detailed report
$report = EmailFixer::diagnose('usergmail.com');
// $report->original → "usergmail.com"
// $report->fixed → "[email protected]"
// $report->appliedFixers → ["InsertMissingAt"]
// $report->isValid → true
// $report->wasModified → true
// Batch processing
$reports = EmailFixer::fixMany([
'user@gmail,com',
'admin@yahoo',
'[email protected].',
]);
use PlinCode\LaravelEmailFixer\Rules\SanitizedEmail;
public function rules(): array
{
return [
'email' => ['
// Enable strict RFC validation and garbage rejection
new SanitizedEmail(strict: true, rejectGarbage: true)
// With Italian locale
new SanitizedEmail(locale: 'it')
use PlinCode\LaravelEmailFixer\Middleware\SanitizeEmails;
// In a route group
Route::middleware(SanitizeEmails::class)->group(function () {
Route::post('/register', [RegisterController::class, 'store']);
});
// Or globally in bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->append(SanitizeEmails::class);
})
// In your registration form request
public function rules(): array
{
return [
'email' => ['
use PlinCode\LaravelEmailFixer\Facades\EmailFixer;
$emails = array_column($rows, 'email');
$reports = EmailFixer::fixMany($emails);
foreach ($reports as $index => $report) {
if ($report->isValid) {
$rows[$index]['email'] = $report->fixed;
} else {
$failed[] = $rows[$index]; // flag for manual review
}
}
// Via config (config/email-fixer.php)
'locale' => 'it',
// Or at runtime
$fixer = EmailFixer::locale('it');
$fixer->fix('mrossiòlibero'); // "[email protected]"
use PlinCode\LaravelEmailFixer\Contracts\FixerInterface;
class MyCustomFixer implements FixerInterface
{
public function fix(string $email): string
{
// your logic here
return $email;
}
public function name(): string
{
return 'MyCustomFixer';
}
}