How can one effectively test a mail server offline when developing with PHP?

One way to effectively test a mail server offline when developing with PHP is to use a tool like MailCatcher, which acts as a dummy SMTP server that catches all outgoing emails and displays them in a web interface. This allows you to test your email functionality without actually sending emails to real recipients.

// Example code using PHPMailer to send emails to MailCatcher for testing

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

require 'vendor/autoload.php';

// Instantiate PHPMailer
$mail = new PHPMailer(true);

try {
    // Server settings for MailCatcher
    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->Port = 1025;

    // Recipient
    $mail->setFrom('from@example.com', 'Your Name');
    $mail->addAddress('to@example.com', 'Recipient Name');

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'Test Email';
    $mail->Body = 'This is a test email sent from PHP';

    // Send the email
    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}