How can using a Mailer class in PHP improve the efficiency and reliability of sending emails from a form?

Using a Mailer class in PHP can improve the efficiency and reliability of sending emails from a form by encapsulating the email sending functionality into a separate class. This allows for easier maintenance, reuse, and testing of the email sending code. Additionally, a Mailer class can handle error handling, logging, and other features to ensure that emails are sent successfully.

<?php

class Mailer {
    public function sendEmail($to, $subject, $message) {
        // Code to send email using PHP's built-in mail function or a third-party library
    }
}

// Example of using the Mailer class to send an email
$mailer = new Mailer();
$mailer->sendEmail('recipient@example.com', 'Subject', 'Message body');

?>