What considerations should be made when using the PHP mail function on a hosting service?

When using the PHP mail function on a hosting service, it's important to ensure that the hosting service allows outgoing emails and that the server is properly configured to send emails. Additionally, it's essential to sanitize user input to prevent email injection attacks and to handle any potential errors that may occur during the email sending process.

// Example code snippet for sending an email using PHP mail function
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com";

// Sanitize user input
$to = filter_var($to, FILTER_SANITIZE_EMAIL);
$subject = filter_var($subject, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);

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