ddeboer / imap

Object-oriented, fully tested PHP IMAP library
MIT License
889 stars 252 forks source link

Detect message type, text or html #582

Open Spl4x opened 1 month ago

Spl4x commented 1 month ago

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!

Slamdunk commented 1 month ago

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

Spl4x commented 1 month ago

@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 commented 4 weeks ago

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();