What are the best practices for sending HTML emails using PHP to ensure proper rendering?

When sending HTML emails using PHP, it's important to ensure that the email is properly formatted to display correctly across different email clients. To achieve this, use inline CSS styles, include a plain text version of the email, and test the email on various email clients.

<?php
$to = 'recipient@example.com';
$subject = 'HTML Email Test';
$message = '
<html>
<head>
  <style>
    /* Inline CSS styles */
    body { font-family: Arial, sans-serif; }
    h1 { color: #333; }
  </style>
</head>
<body>
  <h1>Hello, this is a test email!</h1>
  <p>This is a sample HTML email sent using PHP.</p>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: sender@example.com' . "\r\n";

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