What is the correct syntax for using the mail() function in PHP for sending emails and handling errors?

When using the mail() function in PHP to send emails, it is important to handle errors that may occur during the process. One common error is when the email fails to send due to incorrect parameters or server configuration issues. To handle errors, you can use the return value of the mail() function to check if the email was sent successfully or not. Additionally, you can use error handling techniques such as try-catch blocks to catch and handle any exceptions that may occur during the email sending process.

// Example code snippet for sending emails with error handling
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";

if (mail($to, $subject, $message)) {
    echo "Email sent successfully.";
} else {
    echo "Email sending failed.";
}