What are the best practices for testing email functionality in a localhost environment without having to constantly upload files to a server?

Testing email functionality in a localhost environment can be challenging as emails cannot be sent from a local server. One way to test email functionality without constantly uploading files to a server is to use a tool like Mailtrap, which intercepts email sent from your localhost and displays it in a web interface for testing purposes.

// Example code using PHPMailer with Mailtrap for testing email functionality in a localhost environment

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

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->isSMTP();
    $mail->Host = 'smtp.mailtrap.io';
    $mail->SMTPAuth = true;
    $mail->Username = 'your_mailtrap_username';
    $mail->Password = 'your_mailtrap_password';
    $mail->Port = 2525;

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

    //Content
    $mail->isHTML(true);
    $mail->Subject = 'Subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

    $mail->send();
    echo 'Email has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}