Why is this mailing not decoded correctly?

I have this code. This is from the Zend Reading Mail example .

$message = $mail->getMessage(1);

// output first text/plain part
$foundPart = null;
foreach (new RecursiveIteratorIterator($mail->getMessage(1)) as $part) {
    try {
        if (strtok($part->contentType, ';') == 'text/plain') {
            $foundPart = $part;
            break;
        }
    } catch (Zend_Mail_Exception $e) {
        // ignore
    }
}
if (!$foundPart) {
    echo 'no plain text part found';
} else {
    echo $foundPart->getContent();
}

What I can get is a message that works great. But trying to decode a message into something readable does not work. I tried Zend_Mime, imap_mime and iconv with no luck.

This is an example of what I get with $foundPart->getContent();

Hall = F3 heim = FAr

He must say "Halló heimúr"

What I would like is just a library where I could “push a button, get bacon” in practice. I mean, I just want to specify the library in the POP3 email field and receive the email in a readable form (without any encoding problems) and attachments.

imap_mime_header_decode()Gives me an array with the same data.
iconv_ mime_ decode()Does the same

- , - , (PHP/Python Perl)

+3
2

- base64. Zend_Mail ( "" ):

... base64, addAttachment() MIME .

- :

echo base64_decode($foundPart->getContent());

: http://framework.zend.com/manual/en/zend.mail.encoding.html

, .

+2

, , Zend_Mail . , Zend_Mail , , . :

$content = $foundPart->getContent();

switch ($foundPart->contentTransferEncoding) {
    case 'base64':
        $content = base64_decode($content);
        break;
    case 'quoted-printable':
        $content = quoted_printable_decode($content);
        break;
}

//find the charset
preg_match('/charset="(.+)"$/', $foundPart->contentType, $matches);
$charset = $matches[1];

if ($charset == 'iso-8859-1') {
    $content = utf8_encode($content); //convert to utf8
}
+13

Source: https://habr.com/ru/post/1707853/


All Articles