What are the benefits of using a Mailer class instead of the mail() function in PHP?

Using a Mailer class instead of the mail() function in PHP offers several benefits such as better encapsulation, easier maintenance, and improved flexibility for sending emails. The Mailer class allows for easier configuration of email settings, supports attachments, and provides a more object-oriented approach to handling email functionality.

<?php
class Mailer {
    public function sendEmail($to, $subject, $message, $attachments = []) {
        // Code to send email using a library like PHPMailer or Swift Mailer
    }
}

// Example of sending an email using the Mailer class
$mailer = new Mailer();
$mailer->sendEmail('recipient@example.com', 'Hello', 'This is a test email');
?>