How can one differentiate between text and HTML emails in PHP and display them correctly?

To differentiate between text and HTML emails in PHP, you can check the email's content type header. If the content type is "text/html", then the email is HTML formatted, otherwise it is plain text. To display them correctly, you can use PHP's built-in functions like `html_entity_decode()` for HTML emails and `nl2br()` for text emails.

// Check the content type of the email
if ($contentType == "text/html") {
    // Display HTML email
    echo html_entity_decode($emailContent);
} else {
    // Display text email
    echo nl2br($emailContent);
}