How can PHP developers ensure error handling and feedback when sending emails using custom functions?

When sending emails using custom functions in PHP, developers can ensure error handling and feedback by implementing try-catch blocks to catch any exceptions thrown during the email sending process. Additionally, they can use the error_reporting() function to set the level of error reporting and display error messages when necessary.

function sendEmail($to, $subject, $message) {
    try {
        // Code to send email
        // If email sending fails, throw an exception
        if (!mail($to, $subject, $message)) {
            throw new Exception('Email sending failed');
        }
        echo 'Email sent successfully';
    } catch (Exception $e) {
        echo 'An error occurred: ' . $e->getMessage();
    }
}

// Set error reporting level
error_reporting(E_ALL);