How can the `mail()` function in PHP be replaced with a custom function?

The `mail()` function in PHP is used to send emails, but it may not always meet specific requirements or limitations. To replace the `mail()` function with a custom function, you can create a new function that handles email sending using a different method, such as SMTP or a third-party email service. This custom function can provide more flexibility and control over the email sending process.

function custom_mail($to, $subject, $message, $headers = '') {
    // Implement your custom email sending logic here, such as using SMTP or a third-party service
    // Example: using PHPMailer library
    require 'path/to/PHPMailerAutoload.php';
    $mail = new PHPMailer;
    $mail->setFrom('from@example.com', 'Your Name');
    $mail->addAddress($to);
    $mail->Subject = $subject;
    $mail->Body = $message;
    
    if(!$mail->send()) {
        return false;
    } else {
        return true;
    }
}

// Usage:
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email sent using custom_mail function.';
$headers = 'From: from@example.com';

if(custom_mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully.';
} else {
    echo 'Email sending failed.';
}