PHP Luminova: Abstract Base Class for Email Templates
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
nullfromgetHtml()orgetText()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:
- Update the order status.
- If the update succeeds, send the email.
- The
OrderTemplatedecides what the email looks like.
Class Definition
- Full namespace:
Luminova\Base\AbstractEmailTemplate - This class implements Luminova\Interface\LazyObjectInterface.
- This class is abstract.
Methods
getSubject
Get the subject of the email.
abstract public function getSubject(): ?stringReturn:
string|null - The subject of the email.
getHtml
Get the HTML body of the email.
abstract public function getHtml(): ?stringReturn:
string|null - Return email template html body.
getText
Get the TEXT body of the email.
abstract public function getText(): ?stringReturn:
string|null - Return email template alt text body.
getFiles
Get the attachments of the email.
abstract public function getFiles(): arrayReturn:
list<array<string, mixed>> - Attachment definitions. Return [] when the message has no attachments.
| Key | Type | Description |
|---|---|---|
path | string | Required absolute filesystem path. |
name | string | Optional filename shown to the recipient. |
encoding | string | Optional transfer encoding. Defaults to base64. |
type | string | Optional MIME type. |
disposition | string | Optional disposition, such as inline. Defaults to attachment. |