How can PHP distinguish between TEXT and HTML emails when using IMAP to fetch emails?
When fetching emails using IMAP in PHP, you can distinguish between TEXT and HTML emails by checking the content type of the email. You can use the PHP IMAP functions to fetch the email body and then check the content type header to determine if the email is in plain text or HTML format.
// Connect to the IMAP server
$mailbox = imap_open('{imap.example.com:993/imap/ssl}INBOX', 'username', 'password');
// Fetch the email
$email_number = 1;
$email_body = imap_fetchbody($mailbox, $email_number, 1);
// Get the content type
$email_header = imap_headerinfo($mailbox, $email_number);
$content_type = $email_header->type;
// Check if the email is HTML or plain text
if ($content_type == 'text/html') {
// HTML email
echo "HTML Email: " . $email_body;
} else {
// Plain text email
echo "Text Email: " . $email_body;
}
// Close the connection
imap_close($mailbox);