Mail

The Mail class provides a simple interface for sending emails. It supports different "transports," allowing you to switch between sending real emails and writing them to a log file for development and testing.

Quick Reference

send()
send(array $message): bool

Sends email message and returns success status

setTransport()
setTransport(string $transport): void

Sets email transport method (mail or log)

getLogEntries()
getLogEntries(int $limit = 100): array

Returns logged email messages for debugging

clearLog()
clearLog(): bool

Clears all logged email entries

Constructor

__construct
__construct(string $storagePath, string $transport = 'log')

Creates a new Mail instance with the specified storage path and transport method.

Parameters

NameTypeRequiredDescription
$storagePath string Yes Path where mail logs will be stored
$transport string No Initial transport method: 'log' (default) or 'mail'

Example

<?php
$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$mail = $schoolKit->mail();
?>

Methods

send
send(array $message): bool

Sends an email. The message array must contain 'to', 'subject', and either 'text' or 'html' content.

Parameters

NameTypeRequiredDescription
$message array Yes An associative array defining the email (e.g., ['to' => '...', 'subject' => '...']).

Returns

true if the email was sent (or logged) successfully.

Example

<?php
$wasSent = $schoolKit->mail()->send([
    'to' => 'parent@example.com',
    'subject' => 'Your Weekly Update',
    'html' => '<h1>Hello!</h1><p>Here is your update.</p>',
    'text' => "Hello!\n\nHere is your update."
]);

if ($wasSent) {
    echo "Email sent successfully.";
}
?>
setTransport
setTransport(string $transport): void

Sets the mail transport to either 'log' (writes emails to storage/mail.log) or 'mail' (uses the standard PHP mail() function).

Parameters

NameTypeRequiredDescription
$transport string Yes The transport to use: 'log' or 'mail'.

Example

<?php
// Use the logging transport for development environments
if (getenv('APP_ENV') === 'development') {
    $schoolKit->mail()->setTransport('log');
}
?>
getLogEntries
getLogEntries(int $limit = 100): array

Retrieves email entries from the mail log file, with the most recent entries first.

Parameters

NameTypeRequiredDescription
$limit int No The maximum number of log entries to return.

Returns

An array of associative arrays, where each entry represents a logged email.

Example

<?php
// View the last 10 emails sent in the development environment
$loggedEmails = $schoolKit->mail()->getLogEntries(10);

foreach ($loggedEmails as $email) {
    echo "Logged email to: " . $email['to'] . " with subject: " . $email['subject'] . "\n";
}
?>
clearLog
clearLog(): bool

Deletes the mail log file from storage.

Returns

true on success or if the file doesn't exist.

Example

<?php
// Clear the log at the beginning of a test suite
$schoolKit->mail()->clearLog();
?>