How can one send HTML-formatted emails using the mail() function in PHP?

To send HTML-formatted emails using the mail() function in PHP, you need to set the appropriate headers in the mail function to indicate that the content is HTML. This can be done by adding the "Content-Type" header with a value of "text/html". Additionally, you need to ensure that your email content is properly formatted with HTML tags.

$to = "recipient@example.com";
$subject = "HTML Email Test";
$message = "<html><body><h1>Hello!</h1><p>This is a test email in HTML format.</p></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);