Luminova Framework

PHP Luminova: Abstract Base Class for Email Templates

Last updated: 2026-11-28 20:57:12

Create reusable email templates with a subject, HTML and text bodies, and file attachments.

The Email Template Abstract Class is a base for implementing reusable email templates. Your subclass implements getSubject(), getHtml(), getText(), and getFiles() to supply the subject, HTML body, plain-text fallback, and attachments for the mailer.


Examples

Creating Your Own Email Template

A template is just a small class that tells Luminova what the email should look like.Extend AbstractEmailTemplate, then return the subject, HTML message, text fallback, and any files you want to attach.

// app/Models/Emails/OrderTemplate.php
namespace App\Models\Emails;

use Luminova\Base\AbstractEmailTemplate;

class OrderTemplate extends AbstractEmailTemplate
{
    public function getSubject(): ?string
    {
        return 'Order Completed';
    }

    public function getHtml(): ?string
    {
        return '<p>Your order has been completed successfully.</p>';
    }

    public function getText(): ?string
    {
        return 'Your order has been completed successfully.';
    }

    public function getFiles(): array
    {
        return [];
    }
}

Return null from getHtml() or getText() to omit that part of the message.


Sending the Email

Now use the template inside your controller.Pass an instance of the template to Mailer::to()->send().

// app/Controllers/Http/OrderController.php

namespace App\Controllers\Http;

use Luminova\Base\Http\AbstractController;
use Luminova\Attributes\Route;
use Luminova\Attributes\Prefix;
use Luminova\Components\Email\Mailer;
use App\Models\Emails\OrderTemplate;
use App\Models\Orders;

#[Prefix('/order/(:base)')]
class OrderController extends AbstractController
{
    #[Route('/order/complete/(:integer)')]
    public function complete(int $orderNumber, Orders $order): int
    {
        $isCompleted = $order->update($orderNumber, [
            'status' => 'completed'
        ]);

        try{
            return $this->view('order_completed', [
                'orderNumber' => $orderNumber,
                'status'      => $isCompleted,
                'message'     => $isCompleted
                    ? 'Thank you for your order!'
                    : 'Order failed!'
            ]);
        } finally {
            if ($isCompleted) {
                Mailer::to('[email protected]')
                    ->send(new OrderTemplate());
            }
        }
    }
}

How it works:

  1. Update the order status.
  2. If the update succeeds, send the email.
  3. The OrderTemplate decides what the email looks like.

Class Definition


Methods

getSubject

Get the subject of the email.

abstract public function getSubject(): ?string

Return:

string|null - The subject of the email.


getHtml

Get the HTML body of the email.

abstract public function getHtml(): ?string

Return:

string|null - Return email template html body.


getText

Get the TEXT body of the email.

abstract public function getText(): ?string

Return:

string|null - Return email template alt text body.


getFiles

Get the attachments of the email.

abstract public function getFiles(): array

Return:

list<array<string, mixed>> - Attachment definitions. Return [] when the message has no attachments.

KeyTypeDescription
pathstringRequired absolute filesystem path.
namestringOptional filename shown to the recipient.
encodingstringOptional transfer encoding. Defaults to base64.
typestringOptional MIME type.
dispositionstringOptional disposition, such as inline. Defaults to attachment.