VOOZH about

URL: https://deepwiki.com/hypervel/mail/8-contracts-and-interfaces

⇱ Contracts and Interfaces | hypervel/mail | DeepWiki


Loading...
Menu

Contracts and Interfaces

Purpose and Scope

This document describes the core interfaces (contracts) that define the public API of the hypervel/mail package. These contracts establish the fundamental behaviors required for sending mail, defining mail messages, and queue integration. Understanding these interfaces is essential for implementing custom transports, creating mailables, or extending the mail system.

For information about concrete implementations of these contracts, see Core Architecture. For guidance on creating mailable classes that implement the Mailable contract, see Working with Mailables. For queue integration details, see Queue Integration.


Contract Architecture Overview

The mail system defines three primary contracts that work together to provide a complete mail sending abstraction:


Sources: src/Contracts/Mailable.php src/Contracts/Mailer.php src/Contracts/MailQueue.php


The Mailable Contract

The Mailable contract defines the interface for classes that represent email messages. It combines message definition, recipient configuration, and dispatch methods into a single fluent interface.

Interface Definition

MethodParametersReturn TypePurpose
send()Factory|Mailer $mailer?SentMessageSend the message immediately
queue()QueueFactory $queuemixedQueue the message for async delivery
later()DateInterval|DateTimeInterface|int $delay, QueueFactory $queuemixedQueue with delay
to()array|object|string $address, ?string $namestaticSet primary recipients
cc()array|object|string $address, ?string $namestaticSet carbon copy recipients
bcc()array|object|string $address, ?string $namestaticSet blind carbon copy recipients
locale()string $localestaticSet message locale
mailer()?string $mailerstaticSet mailer instance to use

Sources: src/Contracts/Mailable.php12-53

Method Details

Send Methods

The contract defines three dispatch methods:

send(Factory|Mailer $mailer) - Sends the message immediately using the provided mailer or factory. Accepts either a Factory (which resolves to a Mailer) or a Mailer instance directly. Returns a SentMessage on success or null if sending is prevented by event listeners.

queue(QueueFactory $queue) - Queues the message for asynchronous delivery. The message is wrapped in a SendQueuedMailable job and pushed to the configured queue. Returns the job ID or queue-specific identifier.

later(DateInterval|DateTimeInterface|int $delay, QueueFactory $queue) - Similar to queue(), but delays execution. The $delay parameter accepts an integer (seconds), DateInterval, or DateTimeInterface.

Sources: src/Contracts/Mailable.php14-27

Recipient Configuration

Recipient methods (to(), cc(), bcc()) accept flexible address formats:

  • String: Single email address ("user@example.com")
  • String with name: Email and optional name parameter ("user@example.com", "John Doe")
  • Array: Multiple addresses (["user1@example.com", "user2@example.com"])
  • Object: Any object implementing __toString() or having email property

All three methods return static for fluent chaining.

Sources: src/Contracts/Mailable.php29-42

Configuration Methods

locale(string $locale) - Sets the locale for rendering the message content. This affects view translation and localized date/time formatting. Returns static for chaining.

mailer(?string $mailer) - Specifies which mailer configuration to use. Corresponds to keys in the mail.php mailers array. Pass null to use the default mailer. Returns static for chaining.

Sources: src/Contracts/Mailable.php44-52

Usage Example Pattern



The Mailer Contract

The Mailer contract defines the interface for components that send mail. It provides both low-level sending methods and high-level fluent interfaces for building messages.

Interface Definition

MethodParametersReturn TypePurpose
to()mixed $usersPendingMailBegin fluent mail to recipients
bcc()mixed $usersPendingMailBegin fluent mail with BCC
raw()string $text, mixed $callback?SentMessageSend plain text message
send()array|Mailable|string $view, array $data, Closure|string|null $callback?SentMessageSend message with view
sendNow()array|Mailable|string $mailable, array $data, Closure|string|null $callback?SentMessageSend synchronously

Sources: src/Contracts/Mailer.php11-37

Method Details

Fluent Interface Methods

to(mixed $users) - Initiates a fluent mail building interface. Returns a PendingMail instance that allows chaining recipient configuration before sending. The $users parameter accepts the same flexible formats as the Mailable contract recipient methods.

bcc(mixed $users) - Similar to to(), but initializes with BCC recipients. Returns a PendingMail instance for fluent chaining.

Sources: src/Contracts/Mailer.php13-21

Direct Sending Methods

raw(string $text, mixed $callback) - Sends a plain text email without view rendering. The $text parameter contains the raw message body. The $callback can be a closure that receives a message instance for configuration, or a string specifying the recipient address.

send(array|Mailable|string $view, array $data, Closure|string|null $callback) - Sends a message using a view template. Accepts three usage patterns:

  • Mailable instance: Sends the mailable directly
  • String view name: Renders the view with $data and configures via $callback
  • Array: Specifies ['html' => 'view.html', 'text' => 'view.text'] for dual-part messages

sendNow(array|Mailable|string $mailable, array $data, Closure|string|null $callback) - Forces synchronous sending, bypassing queue configuration. Useful when a mailable class has queue settings but you need immediate delivery.

Sources: src/Contracts/Mailer.php23-37

Mailer vs Mailable Relationship


The Mailer acts as the sending engine, while Mailable represents the message. A Mailable requires a Mailer to send, but application code can interact with either interface depending on the use case.

Sources: src/Contracts/Mailer.php src/Contracts/Mailable.php


The MailQueue Contract

The MailQueue contract defines queue-specific mail operations. This interface is typically implemented alongside the Mailer interface to provide queue integration.

Interface Definition

MethodParametersReturn TypePurpose
queue()array|MailableContract|string $view, ?string $queuemixedQueue message to default queue
later()DateInterval|DateTimeInterface|int $delay, array|MailableContract|string $view, ?string $queuemixedQueue with delay

Sources: src/Contracts/MailQueue.php11-22

Method Details

queue(array|MailableContract|string $view, ?string $queue) - Queues an email for asynchronous sending. The $view parameter accepts the same formats as Mailer::send(). The optional $queue parameter specifies which queue to use (e.g., 'emails', 'high-priority'), defaulting to the configured default queue.

later(DateInterval|DateTimeInterface|int $delay, array|MailableContract|string $view, ?string $queue) - Similar to queue(), but adds a delay before processing. The delay can be specified as:

  • Integer: Seconds to delay
  • DateInterval: Relative time interval
  • DateTimeInterface: Absolute timestamp when the job should run

Sources: src/Contracts/MailQueue.php13-21

Queue Integration Pattern


The MailQueue contract enables the Mailer implementation to create queue jobs that wrap mailables for deferred processing.

Sources: src/Contracts/MailQueue.php


The Factory Contract

The Factory contract defines the interface for creating and resolving Mailer instances. While not provided as a separate file, it is referenced in the Mailable contract.

Contract Usage

Based on the Mailable interface src/Contracts/Mailable.php17 the Factory contract is used as an alternative to Mailer in the send() method signature:


This union type allows mailables to accept either:

  • A Factory instance (e.g., MailManager) that resolves the appropriate mailer
  • A direct Mailer instance for explicit control

Factory Pattern Implementation


The Factory contract enables the manager pattern, where a single factory creates multiple mailer instances based on configuration. For implementation details, see Mail Manager and Factory Pattern.

Sources: src/Contracts/Mailable.php17


Contract Implementations

The following table maps each contract to its primary implementation(s):

ContractPrimary ImplementationLocationNotes
MailableMailable traitsrc/Mailable.phpMixed into custom mailable classes
MailerMailer classsrc/Mailer.phpInstantiated by MailManager
MailQueueMailer classsrc/Mailer.phpSame class implements both contracts
FactoryMailManager classsrc/MailManager.phpSingleton factory service

Implementation Pattern


Sources: src/Contracts/Mailable.php src/Contracts/Mailer.php src/Contracts/MailQueue.php


Contract Interaction Flow

The contracts work together to provide flexible mail sending patterns:


This diagram illustrates three primary usage patterns:

  1. Fluent Interface: Use Factory to create PendingMail, configure recipients, then send
  2. Direct Mailable Send: Configure a Mailable instance, call send() with a Factory or Mailer
  3. Queued Sending: Configure a Mailable, call queue() with a QueueFactory

Sources: src/Contracts/Mailable.php src/Contracts/Mailer.php src/Contracts/MailQueue.php


Implementing Custom Contracts

Creating a Custom Mailable Implementation

To implement the Mailable contract without using the provided trait:


Creating a Custom Mailer Implementation

To implement the Mailer contract with custom sending logic:


Sources: src/Contracts/Mailable.php src/Contracts/Mailer.php


Type Safety and Return Values

Return Type Semantics

All contracts use specific return types that convey important information:

Return TypeMeaningNull Allowed?Use Case
?SentMessageSent message or preventedYesMay return null if event listener prevents sending
PendingMailFluent configurationNoAlways returns for chaining
staticFluent self-referenceNoEnables method chaining on instance
mixedQueue job IDNoQueue-specific identifier format varies

Nullable SentMessage Pattern

The ?SentMessage return type in send() methods indicates that sending can be prevented by event listeners:


For more information on event-based sending prevention, see Event System.

Sources: src/Contracts/Mailable.php17 src/Contracts/Mailer.php26-36


Summary

The hypervel/mail package defines four core contracts that establish clear separation of concerns:

  • Mailable: Defines message structure and dispatch methods
  • Mailer: Provides sending engine and fluent interfaces
  • MailQueue: Enables queue integration for async delivery
  • Factory: Creates and resolves mailer instances from configuration

These contracts enable:

  • Type-safe mail operations with IDE autocomplete
  • Dependency injection and testability
  • Custom implementations for specialized requirements
  • Clear boundaries between message definition and sending logic

For concrete usage examples, see Working with Mailables. For transport implementation details, see Transport System Overview.

Sources: src/Contracts/Mailable.php src/Contracts/Mailer.php src/Contracts/MailQueue.php