How can PHP be used to send HTML emails with alternative text for non-HTML clients?

To send HTML emails with alternative text for non-HTML clients, you can use the PHP `mail()` function along with MIME headers to include both HTML and plain text versions of the email content. This way, email clients that do not support HTML will display the plain text version instead.

$to = 'recipient@example.com';
$subject = 'HTML Email with Alternative Text';
$html_message = '<html><body><h1>Hello World!</h1><p>This is an HTML email.</p></body></html>';
$text_message = 'Hello World! This is the plain text version of the email.';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

// Create a boundary for the multipart message
$boundary = md5(time());

// Add the plain text version
$message = "--$boundary\r\n";
$message .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $text_message . "\r\n";

// Add the HTML version
$message .= "--$boundary\r\n";
$message .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $html_message . "\r\n";

// Add the boundary to indicate the end of the message
$message .= "--$boundary--";

// Send the email
mail($to, $subject, $message, $headers);