Question

i have the following function:

public function getMails()
{
    $mails = array();
    $numMessages = imap_num_msg($this->imap);
    for ($i = 1; $i <= $numMessages; $i++)
    {
        $header = imap_header($this->imap, $i);

        $fromInfo = $header->from[0];
        $replyInfo = $header->reply_to[0];

        $details = array(
            "fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
                ? $fromInfo->mailbox . "@" . $fromInfo->host : "",
            "fromName" => (isset($fromInfo->personal))
                ? $fromInfo->personal : "",
            "replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
                ? $replyInfo->mailbox . "@" . $replyInfo->host : "",
            "replyName" => (isset($replyInfo->personal))
                ? $replyInfo->personal : "",
            "subject" => (isset($header->subject))
                ? $header->subject : "",
            "udate" => (isset($header->udate))
                ? $header->udate : ""
        );

        $bodyText = imap_fetchbody($this->imap,$i,1.2);
        if(!strlen($bodyText)>0){
            $bodyText = imap_fetchbody($this->imap,$i,1);
        }
        $details['body'] = $bodyText;

        $uid = imap_uid($this->imap, $i);
        $current_mail = array('header'=>$header, 'from'=>$fromInfo, 'reply'=>$replyInfo, 'details'=>$details);

        $mails[$i] = $current_mail;
    } 
}

However there is a problem with the body text.

This is a test mail that i sendt from my email that looks like this:

Hello world

Med venlig hilsen

Marc Rasmussen

Besøg mig på MarcRasmussen.dk                     

However the body text is looks like this when taken from imap:

    Hello world=0A=
=0A=
Med venlig hilsen=0A=
=0A=
Marc Rasmussen=0A=
=0A=
Bes=F8g mig p=E5 MarcRasmussen.dk                     =

is there any buildin method in PHP to fix this issue?

Was it helpful?

Solution

You have to check the Content-Transfer-Encoding header of the MIME part you are working on (hint: it's available in IMAP's BODYSTRUCTURE) and decode it yourself. The two most common encoding are quoted-printable and base64. See RFC 2045, chapter 6 for details.


OTHER TIPS

You could try to use the $options parameter of the imap_fetchbody function. Maybe with flag FT_INTERNAL combined with utf8_encode:

$bodyText = utf8_encode(imap_fetchbody($this->imap,$i,1, FT_INTERNAL));

I hope it works for you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top