How can PHP be used to generate multi-part emails for sending complex content like HTML pages?

To generate multi-part emails for sending complex content like HTML pages using PHP, you can use the PHP `mail()` function along with the `Content-Type: multipart/mixed` header to include both text and HTML versions of the email. This allows the recipient's email client to choose the most appropriate version to display.

$to = 'recipient@example.com';
$subject = 'Complex Content Email';
$message = 'This is the text version of the email.';
$html_message = '<html><body><h1>This is the HTML version of the email.</h1></body></html>';

$boundary = md5(time());

$headers = "From: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message . "\r\n";

$body .= "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $html_message . "\r\n";

$body .= "--$boundary--";

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