Email Services

Method: sendEmail

A robust wrapper for PHPMailer that handles HTML content, attachments, and SMTP configurations seamlessly. Ensure high deliverability for your transactional emails.

SMTP Configuration

Configure these environment variables in your .env file to enable email transport.

Variable Description
SMTP_HOST The SMTP server address (e.g., smtp.gmail.com).
SMTP_USERNAME Your SMTP username / email address.
SMTP_PASSWORD App password or SMTP password.
SMTP_PORT Port (typically 587 for TLS or 465 for SSL).
MAIL_FROM Default sender email address.

Parameters

  • recipientEmail

    Target email address string.

  • subject

    Email subject line.

  • body

    HTML content string. Can include inline CSS.

Return Value

Boolean

Returns true on success.

Throws an Exception on failure containing the PHPMailer error info.

Example Implementation

This example demonstrates using the improved Mailer class with its fluent API within a route handler.

src/app/sendemail/index.php
<?php
    
use Lib\PHPMailer\Mailer;
use Lib\StateManager;
    
// Retrieve states
$errorMessage = StateManager::getState('errorMessage', ['status' => false, 'message' => '']);
$name         = StateManager::getState('name');
$email        = StateManager::getState('email');
$subject      = StateManager::getState('subject');
$message      = StateManager::getState('message');
    
function sendEmail($data)
{
    // Update state inputs
    foreach ($data as $key => $value) {
        StateManager::setState($key, $value);
    }
    
    // Basic Validation
    foreach ($data as $key => $value) {
        if (empty($value)) {
            StateManager::setState('errorMessage', ['status' => false, 'message' => "Please fill out the $key field."]);
            return;
        }
    }
    
    $body = <<<EOT
        <h1>New message from {$data->name}</h1>
        <p>Email: {$data->email}</p>
        <p>Message: {$data->message}</p>
EOT;

    try {
        $mailer = new Mailer();
        
        // Using the new Fluent API
        $mailer->to($data->email, $data->name)
               ->subject($data->subject)
               ->html($body)
               ->send();

        StateManager::setState('errorMessage', ['status' => true, 'message' => 'Email sent successfully.']);
        
        // Clear form
        foreach (['name', 'email', 'subject', 'message'] as $key) StateManager::setState($key, '');

    } catch (Exception $e) {
        StateManager::setState('errorMessage', ['status' => false, 'message' => 'Failed to send: ' . $e->getMessage()]);
    }
}
?>

<div class="w-full max-w-md mx-auto p-6">
    <form class="space-y-4" onsubmit="sendEmail" pp-suspense="{'disabled': true}">
        <h1 class="text-2xl font-bold">Contact Us</h1>
        
        <p class="<?= $errorMessage['status'] ? 'text-chart-2' : 'text-destructive' ?>">
            <?= $errorMessage['message'] ?>
        </p>

        <input class="border border-border bg-input rounded-md p-2 w-full" name="email" placeholder="Email" value="<?= $email ?>" />
        <input class="border border-border bg-input rounded-md p-2 w-full" name="subject" placeholder="Subject" value="<?= $subject ?>" />
        <textarea class="border border-border bg-input rounded-md p-2 w-full min-h-[100px]" name="message"><?= $message ?></textarea>
        
        <button type="submit" class="bg-primary text-primary-foreground rounded-md px-4 py-2 w-full" pp-suspense="Sending...">
            Send Email
        </button>
    </form>
</div>

Pro Tip: pp-suspense

Use the pp-suspense attribute to automatically show loading states and disable buttons while the PHP backend is processing your email request.