Are there best practices for using the mail() function in PHP to send formatted emails?

When using the mail() function in PHP to send formatted emails, it is best practice to set the appropriate headers for the email, such as Content-Type and MIME-Version, to ensure that the email is displayed correctly in the recipient's inbox. Additionally, you should use HTML tags to format the email content, including styling with CSS if needed. Finally, make sure to properly sanitize any user input to prevent security vulnerabilities.

$to = "recipient@example.com";
$subject = "Test email";
$message = "<html><body><h1>Hello, this is a test email!</h1></body></html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: sender@example.com" . "\r\n";

mail($to, $subject, $message, $headers);