How can a Mailer class in PHP improve the process of sending emails?

Sending emails in PHP can be a complex process, requiring multiple lines of code to set up headers, recipients, and message content. By creating a Mailer class, we can encapsulate all the email sending functionality into a single class, making it easier to send emails throughout our application. This can improve code organization, reusability, and maintainability.

<?php

class Mailer {
    public function sendEmail($to, $subject, $message) {
        $headers = 'From: your_email@example.com' . "\r\n" .
            'Reply-To: your_email@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
    }
}

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

?>