What are the best practices for styling HTML emails with CSS in PHP?

Styling HTML emails with CSS in PHP can be tricky due to email clients' inconsistent support for CSS. To ensure your email displays correctly across different clients, it's best to inline your CSS styles directly into your HTML code. This can be achieved using PHP libraries like "CSS inliner" to automatically convert your CSS styles into inline styles.

<?php
require 'vendor/autoload.php'; // Include the CSS inliner library

$css = file_get_contents('styles.css'); // Load your CSS styles
$html = file_get_contents('email_template.html'); // Load your HTML email template

$inliner = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles($html, $css);
$inlinedHtml = $inliner->convert();

echo $inlinedHtml; // Output the HTML with inline CSS styles
?>