MagratheaPHP2
Up to date Narrative last written —; no source changes since. Method signatures below are reflected live.

MagratheaMail — Email Functionality

Files: src/MagratheaMail.php, src/MagratheaMailSMTP.php Namespace: Magrathea2

Email sending with support for both PHP's native mail() and SMTP via PHPMailer. Supports HTML and plain-text messages, multiple recipients, simulation mode, and fluent interface.


MagratheaMail (Native mail())

Properties

PropertyTypeDescription
$tostringRecipient(s)
$fromstringSender address
$replyTostringReply-to address
$htmlMessagestringHTML email body
$txtMessagestringPlain-text email body
$subjectstringEmail subject
$errorstringLast error message
$simulateboolIf true, don't actually send

Methods

SetTo(string|array $var): MagratheaMail

Set the recipient(s). Pass a string address or an array of addresses.

$mail->SetTo("john@example.com");
$mail->SetTo(["a@example.com", "b@example.com"]);

SetFrom(string $from, ?string $reply = null): MagratheaMail

Set the sender address and optional reply-to.

$mail->SetFrom("noreply@myapp.com", "support@myapp.com");

SetReplyTo(string|array $var): MagratheaMail

Set only the reply-to address.

$mail->SetReplyTo("support@myapp.com");

SetSubject(string $subject): MagratheaMail

$mail->SetSubject("Welcome to MyApp!");

SetHTMLMessage(string $message): MagratheaMail

Set the HTML version of the email body.

$mail->SetHTMLMessage("<h1>Hello!</h1><p>Welcome aboard.</p>");

SetTXTMessage(string $message): MagratheaMail

Set the plain-text fallback version.

$mail->SetTXTMessage("Hello! Welcome aboard.");

SetNewEmail(string $to, string $from, string $subject): MagratheaMail

Convenience method to set recipient, sender, and subject in one call.

$mail->SetNewEmail("user@example.com", "noreply@myapp.com", "Your account");

Simulate(): MagratheaMail

Enable simulation mode — all other methods work normally but Send() does not actually deliver the email.

$mail->Simulate(); // safe for dev/testing

Validate(): bool

Validates that required fields (to, from, subject, message) are set.

if (!$mail->Validate()) {
    echo $mail->GetError();
}

Send(): bool

Send the email. Returns true on success, false on failure.

$result = $mail->Send();
if (!$result) {
    Logger::Instance()->Log("Email failed: " . $mail->GetError());
}

GetError(): string

Returns the last error message from a failed send.

GetInfo(): array

Returns the current email data as an array (to, from, subject, etc.).

__toString(): string

Returns a readable summary of the email object.


Basic Email Example

use Magrathea2\MagratheaMail;

$mail = new MagratheaMail();
$mail->SetTo("customer@example.com")
     ->SetFrom("noreply@myshop.com")
     ->SetSubject("Order Confirmation #12345")
     ->SetHTMLMessage("
         <h2>Thank you for your order!</h2>
         <p>Your order #12345 has been received.</p>
     ")
     ->SetTXTMessage("Thank you for your order! Order #12345 has been received.")
     ->Send();

MagratheaMailSMTP (SMTP via PHPMailer)

For production use, SMTP is more reliable than PHP's native mail().

Configuration (magrathea.conf)

[mail]
host     = smtp.gmail.com
port     = 587
username = myapp@gmail.com
password = $=SMTP_PASSWORD
from     = myapp@gmail.com

Usage

use Magrathea2\MagratheaMailSMTP;

$mail = new MagratheaMailSMTP();
$mail->SetTo("customer@example.com")
     ->SetSubject("Password Reset")
     ->SetHTMLMessage("<p>Click <a href='...'>here</a> to reset your password.</p>")
     ->Send();

MagratheaMailSMTP shares the same fluent interface as MagratheaMail but routes delivery through the configured SMTP server using PHPMailer.


SMTP Array Configuration

You can also pass SMTP settings directly without relying on config:

$mail = new MagratheaMailSMTP();
$mail->smtpArr = [
    "host"     => "smtp.sendgrid.net",
    "port"     => 587,
    "username" => "apikey",
    "password" => $_ENV["SENDGRID_KEY"],
    "from"     => "noreply@myapp.com",
];
$mail->SetTo("user@example.com")
     ->SetSubject("Hello!")
     ->SetHTMLMessage("<p>Hello from SMTP!</p>")
     ->Send();

Development Pattern: Simulate in Non-Production

use Magrathea2\MagratheaMail;
use Magrathea2\Config;

$mail = new MagratheaMail();

// Only actually send in production
if (Config::Instance()->GetEnvironment() !== "production") {
    $mail->Simulate();
}

$mail->SetTo($user->email)
     ->SetFrom("noreply@myapp.com")
     ->SetSubject("Welcome!")
     ->SetHTMLMessage(renderTemplate("welcome.html", ["user" => $user]))
     ->Send();

Notes

Class Reference — MagratheaMail

Magrathea2\MagratheaMail implements Stringable /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/MagratheaMail.php

MagratheaEmail: function that manages e-mail sends, building headers and sending e-mails

GetError(): string

if an error happened, it's this way you're gonna get it!

GetInfo(): array

returns info about the e-mail

Send(): bool

now we send it!

SetFrom($from, $reply = null): Magrathea2\MagratheaMail

Who are you pretending to be?

ParamTypeDefault
$from mixed required
$reply mixed null
SetHTMLMessage($message): Magrathea2\MagratheaMail

Set Message as HTML

ParamTypeDefault
$message mixed required
SetNewEmail($to, $from, $subject): Magrathea2\MagratheaMail

Ok, I'm in a hurry and don't want to set everything... can you give me all of this in a single function? YES, I CAN!

ParamTypeDefault
$to mixed required
$from mixed required
$subject mixed required
SetReplyTo($var): Magrathea2\MagratheaMail

Who should be replied?

ParamTypeDefault
$var mixed required
SetSubject($subject): Magrathea2\MagratheaMail

What the fuck are we talking about?

ParamTypeDefault
$subject mixed required
SetTXTMessage($message): Magrathea2\MagratheaMail

Set Message as TXT

ParamTypeDefault
$message mixed required
SetTo($var): Magrathea2\MagratheaMail

Who's the guy(s) you have been contacting, huh?

ParamTypeDefault
$var mixed required
Simulate(): Magrathea2\MagratheaMail

just simulate the error

Validate(): bool

Check if there's any visible errors in the e-mail preparation

Class Reference — MagratheaMailSMTP

Magrathea2\MagratheaMailSMTP extends MagratheaMail implements Stringable /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/MagratheaMailSMTP.php
GetInfo(): array

returns info about the e-mail

LoadSMTP(): PHPMailer\PHPMailer\PHPMailer

Loads SMTP inside PHPMailer

Send(): bool

now we send it!

SetSMTP($host, $port, $user, $pass): Magrathea2\MagratheaMailSMTP

Sets SMTP info

ParamTypeDefault
$host mixed required
$port mixed required
$user mixed required
$pass mixed required
SetSMTPArray($arr): Magrathea2\MagratheaMailSMTP

Sets SMTP array

ParamTypeDefault
$arr mixed required
Validate(): bool

Check if there's any visible errors in the e-mail preparation

Examples