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
Constructor
Creates a new Mail instance with the specified storage path and transport method.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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
Sends an email. The message array must contain 'to', 'subject', and either 'text' or 'html' content.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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.";
}
?>
Sets the mail transport to either 'log' (writes emails to storage/mail.log
) or 'mail' (uses the standard PHP mail()
function).
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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');
}
?>
Retrieves email entries from the mail log file, with the most recent entries first.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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";
}
?>
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();
?>