imap icon indicating copy to clipboard operation
imap copied to clipboard

Detect message type, text or html

Open Spl4x opened this issue 1 year ago • 3 comments

Friends

Is there any function or method to detect if a message is HTML or TEXT?

For example:

if(condition){
	$body = $message->getBodyHtml();
} else {
	$body = $message->getBodyText();
}

Or the possibility of retrieving the complete message regardless of the type with a single function.

It happens to me that a message, for example, a response to a server error (Delivery Status Notification (Failure)) comes with the first part in text and the second part in html. When I retrieve the message through the library, it only detects part of the content.

Thanks!

Spl4x avatar Oct 28 '24 17:10 Spl4x

Retrieve both bodies and check which one isn't null?

Slamdunk avatar Oct 29 '24 07:10 Slamdunk

@Slamdunk

In most cases both functions return a valid value.

It would be ideal for the getBodyHtml() function to return null if it is not HTML, if so you could use:

$body = $message->getBodyHtml() ?? $message->getBodyText();

But this is not the case, both getBodyHtml() and getBodyText() return the formatted message.

I have cases where, for example, messages arrive in text, but they have an EML file attached with the HTML message. The ddeboer library attaches this attachment as if it were part of the received message.

Spl4x avatar Oct 29 '24 13:10 Spl4x

In another library (php-imap) they have 2 functions that solve this problem:

Check if a text body exists $status = $message->hasTextBody();

Check if an html body exists $status = $message->hasHTMLBody();

Source: https://www.php-imap.com/api/message

Additionally, it has a very interesting function, which allows you to add embedded images to messages: $body = $message->mask()->getHTMLBodyWithEmbeddedBase64Images();

Spl4x avatar Oct 30 '24 18:10 Spl4x