PHPMailer SMTP Email

PHPMailer SMTP Email

Test and verify SMTP email delivery using PHPMailer with Composer in the Booking System. Ensure secure, reliable email sending for bookings, notifications, and customer communication.

Folder Structure

Your project MUST look like this:

C:\xampp\htdocs\theassembly\
│
├── index.php
├── composer.json
├── vendor\
│   └── autoload.php

Install PHPMailer via Composer (One-Time)

composer require phpmailer/phpmailer

Composer creates:

vendor/
composer.lock
autoload.php

FULL WORKING index.php

<?php
/**
 * ----------------------------------------------------
 * COMPOSER AUTOLOAD (REQUIRED)
 * ----------------------------------------------------
 */
require __DIR__ . '/vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/**
 * ----------------------------------------------------
 * SEND EMAIL FUNCTION
 * ----------------------------------------------------
 */
function sendEmail($to, $subject, $textMessage)
{
    $from_email = 'booking@sydneystaxi.com.au';
    $from_name  = 'Sydney Taxi Booking System';

    $debug_log = __DIR__ . '/email_debug_assembly.txt';

    file_put_contents($debug_log, "\n" . date('Y-m-d H:i:s') . " - SMTP Attempt\n", FILE_APPEND);
    file_put_contents($debug_log, "To: $to\nSubject: $subject\n", FILE_APPEND);

    try {
        $mail = new PHPMailer(true);

        // SMTP CONFIG
        $mail->isSMTP();
        $mail->Host       = 'mail.gmail.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'booking@gmail.com';
        $mail->Password   = 'booking919191'; // 🔐 move to env in production
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = 587;

        // EMAIL
        $mail->CharSet = 'UTF-8';
        $mail->setFrom($from_email, $from_name);
        $mail->addAddress($to);
        $mail->Subject = $subject;
        $mail->Body    = $textMessage;
        $mail->isHTML(false);

        $mail->send();

        file_put_contents($debug_log, "SMTP SENT SUCCESSFULLY\n--------------------------\n", FILE_APPEND);
        return true;

    } catch (Exception $e) {
        file_put_contents(
            $debug_log,
            "SMTP FAILED: {$mail->ErrorInfo}\n--------------------------\n",
            FILE_APPEND
        );
        return false;
    }
}

/**
 * ----------------------------------------------------
 * TEST EMAIL HANDLER
 * ----------------------------------------------------
 */
$message = '';

if (isset($_POST['test_email'])) {
    $testEmail = $_POST['test_email_address'];

    if (filter_var($testEmail, FILTER_VALIDATE_EMAIL)) {

        $subject = "Email - System";

        $testMessage  = "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
        $testMessage .= "EMAIL SEND SUCCESSFUL\n";
        $testMessage .= "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
        $testMessage .= "Congratulations! Your email system is working.\n\n";
        $testMessage .= "Time: " . date('d/m/Y H:i:s') . "\n";
        $testMessage .= "From: booking@sydneystaxi.com.au\n";
        $testMessage .= "Your booking system can now send emails!\n";
        $testMessage .= "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";

        if (sendEmail($testEmail, $subject, $testMessage)) {
            $message = "Email sent successfully to <b>$testEmail</b>";
        } else {
            $message = "Email failed. Check <b>email_debug_assembly.txt</b>";
        }

    } else {
        $message = "Invalid email address";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>PHPMailer Test</title>
    <style>
        body { font-family: Arial; background: #f5f5f5; }
        .box { width: 420px; margin: 80px auto; padding: 25px; background: #fff; border-radius: 6px; }
        input, button { width: 100%; padding: 10px; margin-top: 10px; }
        button { background: #0a7cff; color: #fff; border: none; cursor: pointer; }
        .msg { margin-top: 15px; font-weight: bold; }
    </style>
</head>
<body>

<div class="box">
    <h2>Email System</h2>

    <form method="post">
        <input type="email" name="test_email_address" placeholder="Enter email address" required>
        <button type="submit" name="test_email">Send Test Email</button>
    </form>

    <?php if ($message): ?>
        <div class="msg"><?= $message ?></div>
    <?php endif; ?>
</div>

</body>
</html>

THIS WILL NOW WORK

No require_once PHPMailer/src errors
Uses Composer correctly
SMTP debug logging
Test email form
 Clean, production-ready structure