How to get the email body for every email using php

Viewed 79

I have used following code to extract the content of the emails.

// iterate trough those messages
foreach ($result as $mail) {

    if( strpos($mail->subject, 'Other') !== false and !strstr($mail->subject,'RE:') and !strstr($mail->subject,'Re:') ) {


            print_r($mail); 

            
            $mailBody = quoted_printable_decode(imap_fetchbody($mbox,$mail->msgno,1.1)); 
            if($mailBody == '')
            {
                $mailBody = (imap_fetchbody($mbox,$mail->msgno,1));
            }
        
             //echo nl2br(htmlentities(quoted_printable_decode($mailBody)));
            $ClearText = preg_replace( "/\n\s+/", "\n", rtrim(html_entity_decode(strip_tags($mailBody))) );
            echo($ClearText);
            
        
    }
    
}

From that I can have following output.

stdClass Object
(
    [subject] => XXXX
    [from] => XXXX
    [to] => XXXX
    [date] => Wed, 10 Nov 2021 10:47:53 +0000
    [message_id] => <4209edadb0edb07c227d8c0e51f9dad1@10.62.161.24>
    [size] => 49966
    [uid] => 27569
    [msgno] => 11165
    [recent] => 0
    [flagged] => 0
    [answered] => 0
    [deleted] => 0
    [seen] => 1
    [draft] => 0
    [udate] => 1636541276
)
This is the body in plain text for non-HTML mail clientsstdClass Object

In here I can extract all the details of the emails.But for some emails I cannot fetch the body.Like example above, the body shows as This is the body in plain text for non-HTML mail clientsstdClass Object.But for some emails, I can fetch the body. How should I improve this code to get the email body for every email?

0 Answers
Related