Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Email Laravel Package

black/email

PHP 5.4+ value object for safer email handling. Validates email format (throws on invalid), exposes recipient/domain/tld getters, array parsing, and equality checks. Note: relies on FILTER_VALIDATE_EMAIL; limited for non-ASCII and provider rules.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Domain-Driven Design (DDD) Alignment: The package enforces a value object pattern for emails, aligning well with DDD principles. This is particularly useful in Laravel applications where domain modeling is critical (e.g., user accounts, subscriptions, or transactional workflows).
  • Immutable Validation: The strict validation (format-only, no DNS checks) ensures data integrity early in the pipeline, reducing downstream bugs in email-related logic (e.g., user registration, notifications).
  • Laravel Ecosystem Synergy: Complements Laravel’s built-in Illuminate\Support\Facades\Validator or Illuminate\Validation by providing a type-safe, reusable email abstraction layer. Useful for:
    • Form requests (e.g., StoreUserRequest).
    • Domain services (e.g., UserRegistrationService).
    • API payload validation (e.g., CreateUserRequest in Laravel Sanctum/Passport).

Integration Feasibility

  • Low Coupling: The package is a pure value object with no Laravel-specific dependencies, making it trivially integrable into any PHP project.
  • Composer-First: Aligns with Laravel’s dependency management, requiring zero additional tooling.
  • Validation Granularity: Can replace or augment Laravel’s built-in email validation (e.g., Rule::email()) for stricter type safety or custom business rules (e.g., "only allow corporate domains").

Technical Risk

  • No DNS/MX Validation: The package validates format only, which may expose the application to bounce risks (e.g., sending to non-existent addresses). Mitigation:
    • Pair with Laravel’s Mail::send() + Mailgun/SendGrid webhooks for bounce handling.
    • Use a separate package (e.g., egulias/email-validator) for DNS checks if needed.
  • Minimal Adoption: No dependents or stars suggest unproven reliability. Risk mitigation:
    • Write unit tests for edge cases (e.g., user@sub.domain.co.uk, user+tag@domain.com).
    • Monitor for updates (e.g., PHP 8.1+ compatibility).
  • Naming Confusion: Class name EmailEmail is non-idiomatic (likely a typo; should be Email). Risk:
    • Rename in a wrapper class (e.g., App\ValueObjects\Email) to avoid confusion.

Key Questions

  1. Business Requirements:
    • Does the application need DNS/MX validation, or is format validation sufficient?
    • Are emails used in critical workflows (e.g., password resets) where bounce handling is mandatory?
  2. Team Adoption:
    • Will developers prefer type safety (value objects) over Laravel’s dynamic validation?
    • How will this integrate with existing form requests or API validation?
  3. Maintenance:
    • Who will own updates if the package stagnates (e.g., PHP 9.0 support)?
    • Should a fork be created for customizations (e.g., adding DNS checks)?

Integration Approach

Stack Fit

  • Laravel-Specific Use Cases:
    • Form Requests: Replace Rule::email() with new Email\Email($request->email) in App\Http\Requests\StoreUserRequest.
    • Domain Layer: Use as a return type for services (e.g., User::getEmail(): Email).
    • API Contracts: Enforce in OpenAPI/Swagger specs (e.g., type: string, format: emailtype: Email).
  • Non-Laravel PHP: Useful in CLI tools, queues, or microservices where email validation is needed outside HTTP contexts.

Migration Path

  1. Phase 1: Validation Layer
    • Replace Rule::email() with Email\Email in form requests.
    • Example:
      public function rules(): array
      {
          return [
              'email' => ['required', function ($attribute, $value, $fail) {
                  try {
                      new Email\Email($value);
                  } catch (InvalidEmailEmailException) {
                      $fail('The '.$attribute.' must be a valid email address.');
                  }
              }],
          ];
      }
      
  2. Phase 2: Domain Model
    • Replace raw strings in domain objects (e.g., User model) with Email.
    • Example:
      class User {
          private Email $email;
          // ...
      }
      
  3. Phase 3: API Contracts
    • Update DTOs/API resources to use Email type hints.
    • Example (Laravel API Resources):
      public function toArray($request): array
      {
          return [
              'email' => $this->email->getValue(),
          ];
      }
      

Compatibility

  • PHP Version: Supports 5.4+, but Laravel 9+ requires PHP 8.0+. Test for:
    • Type safety (e.g., Email as a scalar type in PHP 8.0+).
    • Constructor behavior (e.g., new Email("test@example.com") vs. Email::fromString()).
  • Laravel Services:
    • Mailable Classes: Can use Email in Mailable constructors for type safety.
    • Notifications: Replace notifiable()->notify(new InvoicePaid($user->email)) with new InvoicePaid($user->email->getValue()).

Sequencing

Priority Task Dependencies
P0 Replace Rule::email() in critical form requests None
P1 Update domain models to use Email type P0
P2 Integrate into API responses/contracts P1
P3 Add DNS validation layer (if needed) External package (e.g., egulias/email-validator)

Operational Impact

Maintenance

  • Pros:
    • Reduced Boilerplate: Centralized validation logic.
    • Type Safety: IDE autocompletion for Email methods (e.g., getValue(), getDomain()).
  • Cons:
    • Dependency Risk: If the package is abandoned, forks or rewrites may be needed.
    • Testing Overhead: Additional unit tests required for edge cases (e.g., Unicode emails, quoted strings).

Support

  • Debugging:
    • Clear exceptions (InvalidEmailEmailException) simplify error handling.
    • Example:
      try {
          $email = new Email\Email($input);
      } catch (InvalidEmailEmailException $e) {
          return back()->withErrors(['email' => 'Invalid format.']);
      }
      
  • Documentation:
    • Lack of Examples: README is minimal; internal docs or a wrapper class may be needed.
    • Laravel-Specific Guides: Create a docs/laravel-integration.md for team onboarding.

Scaling

  • Performance:
    • Minimal Overhead: Validation is O(1) (regex-based).
    • Caching: If DNS validation is added later, cache results (e.g., Illuminate\Support\Facades\Cache).
  • Distributed Systems:
    • Event-Driven: Use Email in event payloads (e.g., UserRegistered event) for consistency.
    • Microservices: Share the package via Composer or monorepo (e.g., Laravel Sail).

Failure Modes

Scenario Impact Mitigation
Invalid Email Submitted User registration fails Use try-catch in form requests.
Package Deprecation Broken builds Fork or migrate to symfony/validator + custom constraints.
DNS Validation Missing Bounced emails Implement webhook handlers (e.g., Mailgun) or add egulias/email-validator.
PHP Version Incompatibility Build failures Pin version in composer.json (e.g., "black/email": "1.0.0").

Ramp-Up

  • Onboarding:
    • Workshop: 1-hour session on value objects vs. dynamic validation.
    • Codelab: Step-by-step migration of a User model and StoreUserRequest.
  • Training:
    • Pair Programming: Review PRs for Email integration.
    • Cheat Sheet: List common patterns (e.g., "How to use Email in a Mailable").
  • Tooling:
    • Static Analysis: Add phpstan rules to enforce Email usage in domain models.
    • CI Checks: Fail builds if Email is used incorrectly (e.g., new Email(null)).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor