Overview

Architecture

WPZylos Mail follows a layered architecture with three main components:

┌─────────────────────────────────────┐
|          MailServiceProvider        |  ← Container registration
+--───────────────────────────────────┤
|            MailManager              |  ← Central service, defaults
+--───────────────────────────────────┤
|             Mailable                |  ← Fluent email builder
+--───────────────────────────────────┤
|            wp_mail()                |  ← WordPress mail transport
+--───────────────────────────────────┘

Mailable (Builder Pattern)

The Mailable class implements the Builder pattern with a fluent interface. It collects email properties (recipients, subject, body, headers, attachments) via chainable method calls, then produces a complete email when build() or send() is called.

Body resolution priority:

  1. Raw HTML (html())
  2. Plain text (text())
  3. PHP view template (view())
  4. Component parts: greeting, lines, and action button

MailManager (Factory/Service Pattern)

MailManager acts as a factory for Mailable instances and a central configuration point. It holds default from address/name and template paths, applying them automatically to every mailable it creates.

MailServiceProvider (Provider Pattern)

MailServiceProvider registers MailManager as a singleton in the WPZylos application container. This follows the standard WPZylos service provider contract.

Design Decisions

Why wp_mail()?

By building on top of WordPress's wp_mail(), the package automatically inherits SMTP configuration from popular plugins like WP Mail SMTP, FluentSMTP, Post SMTP, etc. No need to configure transport settings — just install an SMTP plugin and the mail works.

Why a Built-In HTML Layout?

Most transactional emails need a clean, responsive container. The built-in layout provides a centered, white card on a gray background that works across all major email clients. For full customization, use html() or view() to supply your own markup.

Why Component-Based Building?

The greeting(), line(), and action() methods follow the notification email pattern popularized by Laravel. They allow developers to build professional-looking emails without writing any HTML.

Flow Diagram

Developer Code
    |
    ▼
Mailable->to()->subject()->line()->send()
    |
    ▼
Mailable::build()
    |── buildHeaders()  → Content-Type, From, CC, BCC, etc.
    |── buildBody()     → HTML / Text / View / Components
    |
    ▼
wp_mail($to, $subject, $message, $headers, $attachments)
    |
    ▼
WordPress PHPMailer / SMTP Plugin