What are the advantages of using classes for email functionality in PHP instead of the traditional mail function?

When using classes for email functionality in PHP, it allows for better organization and encapsulation of code related to sending emails. This makes it easier to manage and maintain email functionality within a larger codebase. Additionally, using classes allows for easier reuse of code and the ability to extend functionality through inheritance and interfaces.

<?php

class Email {
    public function sendEmail($to, $subject, $message) {
        // Code to send email using a library like PHPMailer or Swift Mailer
    }
}

$email = new Email();
$email->sendEmail('recipient@example.com', 'Hello', 'This is a test email.');

?>