定制 aliziodev/laravel-gmail-unique 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aliziodev/laravel-gmail-unique

Composer 安装命令:

composer require aliziodev/laravel-gmail-unique

包简介

A Laravel package that normalizes Gmail addresses during validation to prevent duplicate user registrations with Gmail's dot variations and plus aliases, ideal for SaaS applications with trial subscriptions.

README 文档

README

Packagist License Latest Stable Version Total Downloads PHP Version Laravel Version Laravel Version Laravel Version

Laravel Gmail Unique is a package that normalizes Gmail addresses during validation to prevent duplicate user registrations with Gmail's dot variations and plus aliases. Gmail treats addresses like john.doe@gmail.com, johndoe@gmail.com, and john+alias@gmail.com as the same account, but standard validation treats them as different emails.

According to Google's official documentation Google: Getting messages sent to a dotted version of my address , if you add dots to a Gmail address, you'll still get that email. For example, if your email is johnsmith@gmail.com, you own all dotted versions of your address:

This package ensures that your application recognizes these variations as the same email address, preventing duplicate accounts and improving user experience. This is especially recommended for SaaS systems with trial subscriptions to prevent users from creating multiple trial accounts using Gmail variations.

Features

  • Normalizes Gmail addresses by removing dots and plus aliases
  • Prevents duplicate user registrations with Gmail variations
  • Easy integration with Laravel's validation system
  • Configurable for custom domains and email column names
  • Works with Laravel's Eloquent models via a simple trait

Installation

You can install the package via composer:

composer require aliziodev/laravel-gmail-unique

After installation, you can use the package's install command:

php artisan gmail-unique:install

Configuration

After publishing the configuration, you can modify the settings in config/gmail-unique.php :

return [
    // Email domains to normalize (default: gmail.com and googlemail.com)
    'domains' => ['gmail.com', 'googlemail.com'],

    // The column name used for email in your database (default: email)
    'email_column' => 'email',

    // Error message for duplicate Gmail addresses
    'error_message' => 'This email is already taken (normalized Gmail detected).'
];

Usage

Using the Trait

The simplest way to use this package is by adding the HasNormalizedEmail trait to your User model:

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Aliziodev\GmailUnique\Traits\HasNormalizedEmail;

class User extends Authenticatable
{
    use HasNormalizedEmail;

    // ... rest of your model
}

With this trait, your model will automatically:

  1. Normalize Gmail addresses during validation
  2. Prevent saving duplicate Gmail addresses with different dot variations or plus aliases
  3. Allow users to update their own email with different variations of the same normalized address

Using the Facade

This package also provides a Facade for easier access to the Gmail Unique functionality:

<?php

namespace App\Http\Controllers;

use Aliziodev\GmailUnique\Facades\GmailUnique;
use App\Models\User;

class UserController extends Controller
{
    public function checkEmail($email)
    {
        // Normalize an email address
        $normalized = GmailUnique::normalize($email);

        // Check if a normalized version already exists
        $isDuplicate = GmailUnique::isDuplicate($email, User::class, $excludeId = null);

        return [
            'original' => $email,
            'normalized' => $normalized,
            'isDuplicate' => $isDuplicate
        ];
    }
}

Using the Service

If you need more control, you can use the GmailUniqueService directly:

<?php

namespace App\Http\Controllers;

use Aliziodev\GmailUnique\Services\GmailUniqueService;
use App\Models\User;

class UserController extends Controller
{
    protected $gmailService;

    public function __construct(GmailUniqueService $gmailService)
    {
        $this->gmailService = $gmailService;
    }

    public function checkEmail($email)
    {
        // Normalize an email address
        $normalized = $this->gmailService->normalize($email);

        // Check if a normalized version already exists
        $isDuplicate = $this->gmailService->isDuplicate($email, User::class, $excludeId = null);

        return [
            'original' => $email,
            'normalized' => $normalized,
            'isDuplicate' => $isDuplicate
        ];
    }
}

Custom Validation Rules

You can also use the service in custom validation rules:

use Aliziodev\GmailUnique\Services\GmailUniqueService;
use App\Models\User;

// In a form request or controller
$validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                function ($attribute, $value, $fail) {
                    $gmailService = app(GmailUniqueService::class);
                    if ($gmailService->isDuplicate($value, User::class)) {
                         $fail('This email address is already taken.');
                    }
                }
            ],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);

Testing

The package includes comprehensive tests. You can run them with:

./vendor/bin/pest

License

The MIT License (MIT). Please see License File for more information.

Contributing

Contributions are welcome! Please create issues or pull requests on the GitHub repository.

aliziodev/laravel-gmail-unique 适用场景与选型建议

aliziodev/laravel-gmail-unique 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 98 次下载、GitHub Stars 达 15, 最近一次更新时间为 2025 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「saas」 「gmail」 「email-validation」 「email-normalization」 「gmail-alias」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aliziodev/laravel-gmail-unique 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aliziodev/laravel-gmail-unique 我们能提供哪些服务?
定制开发 / 二次开发

基于 aliziodev/laravel-gmail-unique 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 98
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-03